{"id":15711,"date":"2011-02-03T00:01:00","date_gmt":"2011-02-03T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/02\/03\/export-performance-counters-with-powershell-to-ease-tuning\/"},"modified":"2011-02-03T00:01:00","modified_gmt":"2011-02-03T00:01:00","slug":"export-performance-counters-with-powershell-to-ease-tuning","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/export-performance-counters-with-powershell-to-ease-tuning\/","title":{"rendered":"Export Performance Counters with PowerShell to Ease Tuning"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to use Windows PowerShell to export performance counter sets to simplify analysis and tuning.<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" \/><\/p>\n<p>Hey, Scripting Guy! I think that using Windows PowerShell to parse performance counter stuff is pretty cool. However, as much as I am a command line person, there are times when I need to be able to use the GUI. For example, often I need to take a picture for a report I am writing. The report may be justification for my request to purchase additional hardware. As you know, a picture is worth a thousand words, and I am a person of few words. Is there a way that I can use Windows PowerShell to capture performance data, but still be able to perform analysis using graphical tools?<\/p>\n<p>&mdash;LH<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" \/>Hello LH, <\/p>\n<p>Microsoft Scripting Guy, Ed Wilson here. It is warming up a little bit here in Charlotte, North Carolina today. Of course, since it is winter, the snow replacement is rain. That is OK, however, because it makes for a nice, albeit somewhat gloomy, day. I have six hours of meetings today, but the meetings are cool, and I get to talk to some of my friends, so I do not mind. It does mean that I am up early so I can check the <a href=\"mailto:Scripter@Microsoft.Com\">Scripter@Microsoft.Com<\/a> email. With the volume of email I get, I cannot skip too many days before I am overwhelmed. I have a nice cup of English Breakfast tea steeping in my ceramic tea pot, and I am waiting for a couple of hard boiled eggs to come out of our new egg cooker. In the past, I would manually cook eggs. It involved filling a pot with water, covering it while it boiled, placing the eggs in the boiling water, waiting for about 30 minutes while they soaked, removing them from a pan, placing them in a pan full of ice to cool them down, and then finally peeling them. It is a process that consumed nearly an hour. This is fine for a weekend morning, but not too practical for a day when one has six hours worth of meetings; thus the automation of the egg via the egg cooker. Perhaps I can use the following code to automate my egg.<\/p>\n<blockquote>\n<p>Get-Egg &ndash;uncooked | Set-Egg &ndash;property cooked &ndash;value $true<\/p>\n<\/blockquote>\n<p>LH, working with performance counters can be a little like cooking a hardboiled egg. It can be fun, but at times automation needs to enter into the equation. If I am using the <b>Get-Counter<\/b> cmdlet to retrieve performance counter information, and I wish to have the ability to examine the data in the Performance Monitor tool, all I need to do is to pipe the results to the <b>Export-Counter<\/b> cmdlet. In the command that follows, I use the <b>Get-Counter<\/b> cmdlet to retrieve performance counters related to memory. I collect four samples at one-second intervals. I export the collected information to a <b>blg<\/b> formatted file in the <b>C:\\fso<\/b> folder on my computer. This command appears here. (The following command is a single logical command. I added the line continuation character (`) at the end of the first line to break the line to fit the blog format. When the command is typed into the Windows PowerShell console, the line continuation is not required when the command wraps to additional lines.)<\/p>\n<p>The use of the <b>Get-Counter<\/b> cmdlet appears in <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/01\/31\/use-powershell-to-simplify-collecting-performance-information.aspx\">Monday&rsquo;s Hey Scripting Guy! Blog<\/a> post. <\/p>\n<blockquote>\n<p>PS C:\\&gt; Get-Counter -Counter (get-Counter -ListSet memory).paths -MaxSamples 4 `<\/p>\n<p>-SampleInterval 1 | Export-Counter -Path c:\\fso\\memory.blg -FileFormat blg<\/p>\n<p>PS C:\\&gt;<\/p>\n<\/blockquote>\n<p>No output displays in the Windows PowerShell console when the previous command runs. The only way I know the command completed successfully is the absence of error messages. The only way I know that the command completed running is the presence of a fresh Windows PowerShell console command prompt. <\/p>\n<p>I can open the Performance Monitor utility (Start\/All Programs\/Administrative Tools\/Performance Monitor or Start\/Search Perfmon &lt;ENTER&gt;), and import the saved <b>blg<\/b> file. To do this, I right-click <b>Performance Monitor<\/b> in the left pane and select <b>Performance Monitor Properties<\/b> as shown in the following screen shot.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/8877.HSG-2-3-11-01_6A023AB3.jpg\"><img decoding=\"async\" height=\"650\" width=\"604\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/4645.HSG-2-3-11-01_thumb_67E53BEA.jpg\" alt=\"Image of Performance Monitor Properties dialog box\" border=\"0\" title=\"Image of Performance Monitor Properties dialog box\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>The strange thing is that when I press OK, it appears that nothing has happened. This is shown in the following screen shot.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/1031.HSG-2-3-11-02_66A0A30B.jpg\"><img decoding=\"async\" height=\"422\" width=\"604\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/5228.HSG-2-3-11-02_thumb_4540FD6F.jpg\" alt=\"Image of Performance Monitor result\" border=\"0\" title=\"Image of Performance Monitor result\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>If I look closely, however, I will notice that something has changed&mdash;there is no longer a thin red line traversing the bottom of the monitor display. That small red line was the total percentage of processor utilization real-time display. The fact that it no longer displays indicates that the data source has changed. Now it is a simple matter of adding data to the display. I click the green plus (+) sign on the <b>Tool<\/b> menu to add the counters. Only counters that are available from the log file are present in the <b>Add Counter<\/b> dialog box. This is shown in the following screen shot.<i><\/i><\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/8867.HSG-2-3-11-03_1D2E4E50.jpg\"><img decoding=\"async\" height=\"446\" width=\"604\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/4744.HSG-2-3-11-03_thumb_69F214E6.jpg\" alt=\"Image of Add Counter dialog box\" border=\"0\" title=\"Image of Add Counter dialog box\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>After I have selected the counters that I am interested in viewing, the graph displays the recorded data. On the other hand, if I would like to view current data, I can click the small graph icon on the tool menu. This icon is a bit small, and in the following screen shot, I have drawn an arrow to indicate the graph to which I refer.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/8054.HSG-2-3-11-04_6FCCB87F.jpg\"><img decoding=\"async\" height=\"422\" width=\"604\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/1512.HSG-2-3-11-04_thumb_59969D2D.jpg\" alt=\"Image showing graph icon\" border=\"0\" title=\"Image showing graph icon\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>There may be times that I want to be working with my data in Microsoft Excel. The easy way to do this is to export the data as a CSV file. To do this I pipe the data collection that is returned from the <b>Get-Counter<\/b> cmdlet to the <b>Export-Counter<\/b> cmdlet and specify a <b>FileFormat<\/b> value of <b>csv<\/b>. (The following command is a single logical command. I added the line continuation character (`) at the end of the first line to break the line to fit the blog format. When the command is typed into the Windows PowerShell console, the line continuation character is not required when the command wraps to additional lines.)<\/p>\n<blockquote>\n<p>PS C:\\&gt; Get-Counter -Counter (get-Counter -ListSet memory).paths -MaxSamples 4 `<\/p>\n<\/blockquote>\n<blockquote>\n<p>-SampleInterval 1 | Export-Counter -Path c:\\fso\\memory.csv -FileFormat csv<\/p>\n<\/blockquote>\n<blockquote>\n<p>PS C:\\&gt;<\/p>\n<\/blockquote>\n<p>When I double-click the <b>csv<\/b> file, it opens in Microsoft Excel, and I can then manipulate the data. This is shown in the following screen shot.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/1121.HSG-2-3-11-05_1F3B274C.jpg\"><img decoding=\"async\" height=\"419\" width=\"604\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/0363.HSG-2-3-11-05_thumb_44C4A4AD.jpg\" alt=\"Image of Excel data\" border=\"0\" title=\"Image of Excel data\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>One problem with extensive performance monitoring, is that the counter log files can become rather large. To ensure that the counter log files do not eat up all of the disk space, I can use the <b>MaxSize<\/b><i> <\/i>parameter with the <b>Export-Counter<\/b> cmdlet. When the log file reaches the maximum size an error is raised, and displayed to the Windows PowerShell console. The raised error is a non-terminating error, and it is more informational than an actual problem. The <b>MaxSize<\/b><i> <\/i>parameter is an integer, and it is measured in megabytes (MB). Therefore, a <b>MaxSize<\/b> of <b>1<\/b> will create a 1 MB (1024 KB) file in size. <\/p>\n<p>Proper planning of a data gathering session includes selecting the counters, and selecting the interval and number of samples. A test should then be run to determine the size of the log file. From this baseline, the appropriate value of the maximum log file is calculated. When I run performance gathering sessions and I intend to analyze immediately the data, I use the <b>force<\/b> switch to cause the <b>Export-Counter<\/b> cmdlet to overwrite a previously existing log file of the same name. If I am only interested in collecting the most recent data, I use the <b>circular<\/b><i> <\/i>switch and thereby cause the log file to begin overwriting the oldest records when the log file reaches maximum size. The following command obtains the memory performance counters, and attempts to collect 4,000 samples at 1 second intervals. These are then exported to a <b>blg<\/b> file format, but they are limited to a maximum size of 1 MB.<\/p>\n<blockquote>\n<p>PS C:\\&gt; Get-Counter -Counter (get-Counter -ListSet memory).paths -MaxSamples 4000 `<\/p>\n<p>-SampleInterval 1 | Export-Counter -Path c:\\fso\\memory.blg -FileFormat blg -MaxSize 1<\/p>\n<\/blockquote>\n<p>When the log file is full, an error appears in the Windows PowerShell console. This error is shown in the following screen shot.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/7713.HSG-2-3-11-06_3137450C.jpg\"><img decoding=\"async\" height=\"346\" width=\"604\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/8473.HSG-2-3-11-06_thumb_3390C70A.jpg\" alt=\"Image of error message\" border=\"0\" title=\"Image of error message\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>One of the things I enjoy doing with the <b>Export-Counter<\/b> Windows PowerShell cmdlet is using it to perform file type conversions. I can convert a <b>blg<\/b><i> <\/i>file (generated via <b>Export-Counter<\/b> as illustrated earlier, or saved from the Performance Monitor utility) to a CSV file format or to a tab separated value (TSV) file format. To convert a <b>blg<\/b><i> <\/i>file to a different format, I use the <b>InputObject<\/b><i> <\/i>parameter. Here I use the <b>Import-Counter<\/b> cmdlet to import the <b>blg<\/b><i> <\/i>file and to convert it to a <b>PerformanceCounterSampleSet<\/b> object. I then specify the path to store the converted file, as well as specify the <b>FileFormat<\/b> parameter. The following command accomplishes these tasks by importing a <b>blg<\/b> file that was created earlier, and converting the format to a TSV file. (The following command is a single logical command. I added the line continuation character (`) at the end of the first line to break the line to fit the blog format. When the command is typed into the Windows PowerShell console, the line continuation character is not required when the command wraps to additional lines.)<\/p>\n<blockquote>\n<p>PS C:\\&gt; Export-Counter -InputObject (import-counter c:\\fso\\memory.blg) `<\/p>\n<p>-Path c:\\fso\\memory.tsv -FileFormat tsv<\/p>\n<p>PS C:\\&gt;<\/p>\n<\/blockquote>\n<p>The newly created TSV file is shown in the following image.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/2117.HSG-2-3-11-07_721614B0.jpg\"><img decoding=\"async\" height=\"409\" width=\"604\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/2626.HSG-2-3-11-07_thumb_302F2F62.jpg\" alt=\"Image of TSV file\" border=\"0\" title=\"Image of TSV file\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>LH, that is all there is to using the Windows PowerShell <b>Export-Counter<\/b> cmdlet. This also concludes Performance Week. <\/p>\n<p>I invite you to follow me on <a target=\"_blank\" href=\"https:\/\/twitter.com\/scriptingguys\/\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b><span style=\"font-family: Segoe UI;font-size: x-small\"><\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to export performance counter sets to simplify analysis and tuning. Hey, Scripting Guy! I think that using Windows PowerShell to parse performance counter stuff is pretty cool. However, as much as I am a command line person, there are times when I need to be able to use [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,31,60,3,4,45],"class_list":["post-15711","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-operating-system","tag-performance","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to export performance counter sets to simplify analysis and tuning. Hey, Scripting Guy! I think that using Windows PowerShell to parse performance counter stuff is pretty cool. However, as much as I am a command line person, there are times when I need to be able to use [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15711","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=15711"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15711\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=15711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=15711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=15711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}