{"id":8143,"date":"2021-02-08T10:31:04","date_gmt":"2021-02-08T15:31:04","guid":{"rendered":"https:\/\/jdhitsolutions.com\/blog\/?p=8143"},"modified":"2021-02-09T18:37:09","modified_gmt":"2021-02-09T23:37:09","slug":"solving-the-powershell-memory-challenge","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8143\/solving-the-powershell-memory-challenge\/","title":{"rendered":"Solving the PowerShell Memory Challenge"},"content":{"rendered":"\n<p>I hope you tried your hand at this <a href=\"https:\/\/ironscripter.us\/a-memory-reporting-challenge\/\" target=\"_blank\" rel=\"noreferrer noopener\">Iron Scripter PowerShell challenge<\/a> on reporting memory usage. The basic challenge was to find the total percent of working set memory that a specific process or service is using. Here's how I approached it, with my usual disclaimer that my solution is not the only or nor should it be considered authoritative. Still, I hope you pickup a PowerShell scripting tip or technique.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Get-CimInstance<\/h2>\n\n\n\n<p>My first approach used Get-CimInstance, primarily because it can get information from remote computers. The code I'm about to go through are commands I can run at a PowerShell prompt. Once these commands work and produce the results I'm after, I can wrap them up into a function.<\/p>\n\n\n\n<p>First, I need to find how much memory is currently in use.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$computername\u00a0=\u00a0$env:COMPUTERNAME\n$os\u00a0=\u00a0Get-CimInstance\u00a0win32_operatingsystem\u00a0-Property\u00a0TotalVisibleMemorySize,FreePhysicalMemory\u00a0-Computername\u00a0$computername\n$inUseMemory\u00a0=\u00a0($os.TotalVisibleMemorySize\u00a0-\u00a0$os.FreePhysicalMemory)*1KB<\/code><\/pre>\n\n\n\n<p>Let me point out the use the the -Property parameter with Get-CimInstance.  This is a little thing you can do that might positive performance gains. Getting information from the CIM Repository is much like a SQL query. The best and fastest query is one where you only select the properties you need. Since I only need the two memory properties, that is all that I ask for. I also know based on experience and prior research that the values are in KB. Multiplying the value by 1KB turns the value into bytes.<\/p>\n\n\n\n<p>With this value, I can query a specific service and create custom output that calculates the percent working set memory from the total in-use memory.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Get-CimInstance\u00a0win32_process\u00a0-filter\u00a0\"name='sqlservr.exe'\"\u00a0|\nSelect-Object\u00a0-Property\u00a0ProcessID,Name,HandleCount,WorkingSetSize,\n@{Name=\"PctUsedMemory\";Expression\u00a0=\u00a0{($_.WorkingSetSize\/$InUseMemory)\u00a0*\u00a0100}}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"355\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-1024x355.png\" alt=\"\" class=\"wp-image-8144\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-1024x355.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-300x104.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-768x266.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-1536x532.png 1536w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-850x295.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct.png 1988w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>It wouldn't take much to turn that into a simple PowerShell function. <\/p>\n\n\n\n<p>But in the meantime, I'll extend this concept for all processes other than the System Idle.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Get-CimInstance\u00a0-class\u00a0win32_process\u00a0-computername\u00a0$computername\u00a0-filter\u00a0\"Name&lt;>'systemidleprocess'\"\u00a0|\n\u00a0Group-Object\u00a0-property\u00a0Name\u00a0|\n\u00a0ForEach-Object\u00a0{\n\u00a0$stat\u00a0=\u00a0$_.Group\u00a0|\u00a0Measure-Object\u00a0-Property\u00a0WorkingSetSize\u00a0-sum\n\u00a0$_\u00a0|\u00a0Select-Object\u00a0-Property\u00a0Name,Count,@{Name=\"TotalWS\";Expression={$stat.sum}},\n\u00a0@{Name=\"PctUsedMemory\";Expression={[math]::Round(($stat.sum\/$InUseMemory)*100,2)}}\n\u00a0}\u00a0|\u00a0Sort-Object\u00a0-property\u00a0PctUsedMemory\u00a0-Descending|\n\u00a0Select-Object\u00a0-first\u00a010\u00a0|\n\u00a0Format-Table<\/code><\/pre>\n\n\n\n<p>This code is also using the $InUseMemory value I retrieved at the beginning. The processes instances are grouped on the Name property.  This is because I might 10 svchost processes and I want to treat them as one item. <\/p>\n\n\n\n<p>The grouped objects are then enumerated using ForEach-Object. With each item in the group, I'm measuring the total Workingsetsize of the group.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$stat\u00a0=\u00a0$_.Group\u00a0|\u00a0Measure-Object\u00a0-Property\u00a0WorkingSetSize\u00a0-sum<\/code><\/pre>\n\n\n\n<p>I'm then creating a custom object using Select-Object to define new properties. The results are piped to Sort-Object to sort on the custom PctUsedMemory property in descending order. The sorted results are piped to Select-Object to grab  the first 10. And finally, to make it pretty on the screen, I'll format the results as a table.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"448\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-2-1024x448.png\" alt=\"\" class=\"wp-image-8145\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-2-1024x448.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-2-300x131.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-2-768x336.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-2-850x372.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-2.png 1287w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>If I were to turn this into a function, I'd most likely create a true custom object instead of relying on Select-Object.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$computername\u00a0=\u00a0$env:COMPUTERNAME\n$os\u00a0=\u00a0Get-CimInstance\u00a0win32_operatingsystem\u00a0-Property\u00a0TotalVisibleMemorySize,FreePhysicalMemory\u00a0-Computername\u00a0$computername\n$inUseMemory\u00a0=\u00a0($os.TotalVisibleMemorySize\u00a0-\u00a0$os.FreePhysicalMemory)*\u00a01KB\n$grouped\u00a0=\u00a0Get-CimInstance\u00a0win32_process\u00a0-computername\u00a0$computername\u00a0-filter\u00a0\"Name\u00a0&lt;>'system\u00a0idle\u00a0process'\"\u00a0|\nGroup-Object\u00a0-property\u00a0Name\n\n$results\u00a0=\u00a0ForEach\u00a0($item\u00a0in\u00a0$Grouped)\u00a0{\n\u00a0\u00a0\u00a0\u00a0$stat\u00a0=\u00a0$item.Group\u00a0|\u00a0Measure-Object\u00a0-Property\u00a0WorkingSetSize\u00a0-Sum\n\u00a0\u00a0\u00a0\u00a0[pscustomobject]@{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0PSTypeName\u00a0\u00a0\u00a0\u00a0=\u00a0\"ProcessWSPercent\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Name\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$item.Name\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Count\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$item.count\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0TotalWS\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$stat.sum\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0PctUsedMemory\u00a0=\u00a0[math]::Round(($stat.sum\u00a0\/\u00a0$InUseMemory)\u00a0*\u00a0100,\u00a02)\n\u00a0\u00a0\u00a0\u00a0}\n}\n\n$results\u00a0|\u00a0Sort-Object\u00a0-property\u00a0PctUsedMemory\u00a0-Descending\u00a0|\nSelect-Object\u00a0-first\u00a010<\/code><\/pre>\n\n\n\n<p>I think this approach is easier to read in a script file. And if I wanted to, I could create a custom format.ps1xml file to format the results. Perhaps displaying TotalWS in MB instead of bytes, which is what I have now.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"435\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-3-1024x435.png\" alt=\"\" class=\"wp-image-8146\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-3-1024x435.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-3-300x127.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-3-768x326.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-3-1536x652.png 1536w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-3-850x361.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-3.png 1666w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Using Get-Process<\/h2>\n\n\n\n<p>Here's a variation using Get-Process. In this version I'm going to get the total workingset value from all processes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$ps\u00a0=\u00a0Get-Process\n$totalWS\u00a0=\u00a0($ps\u00a0|\u00a0Measure-Object\u00a0-Property\u00a0WS\u00a0-Sum).sum<\/code><\/pre>\n\n\n\n<p>Now I can filter, select, sort and select what I want to see.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$ps\u00a0|\u00a0Where-Object\u00a0{$_.name\u00a0-notmatch\u00a0\"^(system|idle)$\"}\u00a0|\nSelect-Object\u00a0-property\u00a0Name,WS,@{Name=\"PctTotalWS\";Expression\u00a0=\u00a0{($_.ws\/$totalWS)*100}}\u00a0|\nSort-Object\u00a0-Property\u00a0PctTotalWS\u00a0-Descending\u00a0|\nSelect-Object\u00a0-first\u00a010<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/ps-ws.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"488\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/ps-ws-1024x488.png\" alt=\"\" class=\"wp-image-8147\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/ps-ws-1024x488.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/ps-ws-300x143.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/ps-ws-768x366.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/ps-ws-850x405.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/ps-ws.png 1236w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>This variation is displaying results for each process. I can group them in a similar way as I did earlier.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$ps\u00a0|\u00a0Where-Object\u00a0{\u00a0$_.name\u00a0-notmatch\u00a0\"^(system|idle)$\"\u00a0}\u00a0|\nGroup-Object\u00a0-Property\u00a0name\u00a0|\nForEach-Object\u00a0{\n\u00a0\u00a0\u00a0\u00a0$wsSum\u00a0=\u00a0($_.group\u00a0|\u00a0Measure-Object\u00a0-Property\u00a0WS\u00a0-Sum).sum\n\u00a0\u00a0\u00a0\u00a0[PSCustomObject]@{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Name\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$_.Name\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Count\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$_.count\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0TotalWS\u00a0\u00a0\u00a0\u00a0=\u00a0$wsSum\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0PctTotalWS\u00a0=\u00a0[math]::round(($wsSum\u00a0\/\u00a0$totalWS)\u00a0*\u00a0100,\u00a04)\n\u00a0\u00a0\u00a0\u00a0}\n}\u00a0|\u00a0Sort-Object\u00a0-Property\u00a0PctTotalWS\u00a0-Descending\u00a0|\u00a0Select-Object\u00a0-First\u00a05<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/ps-ws-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"312\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/ps-ws-2-1024x312.png\" alt=\"\" class=\"wp-image-8148\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/ps-ws-2-1024x312.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/ps-ws-2-300x91.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/ps-ws-2-768x234.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/ps-ws-2-850x259.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/ps-ws-2.png 1242w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>Again, it wouldn't take much to turn this into an easy to use PowerShell function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Performance Counters<\/h2>\n\n\n\n<p>The last way I might tackle this is by using performance counters. I'll work with a single process that might have multiple instances.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$name\u00a0=\u00a0\"firefox\"\n$Counters\u00a0=\u00a0(Get-Counter\u00a0\"\\process(firefox*)\\Working\u00a0Set\").CounterSamples\n$total\u00a0=\u00a0($counters\u00a0|\u00a0Measure-Object\u00a0-property\u00a0CookedValue\u00a0-sum).Sum\n$committed\u00a0=\u00a0(Get-Counter\u00a0\"\\memory\\committed\u00a0bytes\").Countersamples[0].CookedValue\n$pct\u00a0=\u00a0($total\/$committed)*100<\/code><\/pre>\n\n\n\n<p>You'll notice that I'm using an index of [0] to get the committed bytes value, even though if you run the Get-Counter command you'll only see one result. There is a little quirk with Get-Counter where it returns an array with an empty second value. With these values, I can create a results object.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">[pscustomobject]@{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ProcessName\u00a0=\u00a0$name\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ProcessCount\u00a0=\u00a0$counters.count\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0TotalWS\u00a0=\u00a0[math]::round($total\u00a0\/\u00a01mb,\u00a04)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0PctCommitted\u00a0=\u00a0[math]::round($pct,\u00a04)\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-counter-ws-pct.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"149\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-counter-ws-pct-1024x149.png\" alt=\"\" class=\"wp-image-8150\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-counter-ws-pct-1024x149.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-counter-ws-pct-300x44.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-counter-ws-pct-768x112.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-counter-ws-pct-850x123.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-counter-ws-pct.png 1177w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>I'll extend this concept to all processes, again grouping like processes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">[pscustomobject]@{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ProcessName\u00a0=\u00a0$name\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ProcessCount\u00a0=\u00a0$counters.count\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0TotalWS\u00a0=\u00a0[math]::round($total\u00a0\/\u00a01mb,\u00a04)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0PctCommitted\u00a0=\u00a0[math]::round($pct,\u00a04)\n}<\/code><\/pre>\n\n\n\n<p>My code is using a regular expression to filter out select performance counter results. The rest of the code should look familiar.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$data\u00a0=\u00a0foreach\u00a0($item\u00a0in\u00a0$grouped)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$total\u00a0=\u00a0($item.group\u00a0|\u00a0Measure-Object\u00a0-Property\u00a0CookedValue\u00a0-Sum).Sum\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$pct\u00a0=\u00a0($total\u00a0\/\u00a0$committed)\u00a0*\u00a0100\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[pscustomobject]@{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ProcessName\u00a0\u00a0=\u00a0$item.Name\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ProcessCount\u00a0=\u00a0$item.count\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0TotalWS\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0[math]::round($total\u00a0\/\u00a01mb,\u00a04)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0PctCommitted\u00a0=\u00a0[math]::round($pct,\u00a04)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n$data\u00a0|\u00a0Sort-Object\u00a0TotalWS\u00a0-Descending\u00a0|\u00a0Select-Object\u00a0-first\u00a010<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-counter-ws-pct2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"411\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-counter-ws-pct2-1024x411.png\" alt=\"\" class=\"wp-image-8151\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-counter-ws-pct2-1024x411.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-counter-ws-pct2-300x120.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-counter-ws-pct2-768x308.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-counter-ws-pct2-850x341.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-counter-ws-pct2.png 1408w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>This code should work in Windows under bot Windows PowerShell and PowerShell 7.1.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Services<\/h2>\n\n\n\n<p>Let's wrap up by looking how to approach this from a service angle. A running service is a running process so I should be able to use most of the code I've already shown you. I'll re-run the code to get the $InUseMemory value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$computername\u00a0=\u00a0$env:COMPUTERNAME\n$os\u00a0=\u00a0Get-CimInstance\u00a0win32_operatingsystem\u00a0-Property\u00a0TotalVisibleMemorySize,FreePhysicalMemory\u00a0-Computername\u00a0$computername\n$inUseMemory\u00a0=\u00a0($os.TotalVisibleMemorySize\u00a0-\u00a0$os.FreePhysicalMemory)*\u00a01KB<\/code><\/pre>\n\n\n\n<p>I'll use Get-CimInstance to query the Win32_Service class because it will include the associated process id. Thus for each service I can get the corresponding process and build a custom object.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Get-CimInstance\u00a0-classname\u00a0win32_service\u00a0-Filter\u00a0\"state\u00a0=\u00a0'running'\"\u00a0-Property\u00a0Name,\u00a0ProcessID\u00a0-ComputerName\u00a0$computername\u00a0|\nForEach-Object\u00a0-Begin\u00a0{\n\u00a0\u00a0\u00a0\u00a0<em>#define\u00a0a\u00a0hashtable\u00a0of\u00a0parameters\u00a0to\u00a0splat\u00a0to\u00a0Get-CimInstance\u00a0in\u00a0the\u00a0process\u00a0block<\/em>\n\u00a0\u00a0\u00a0\u00a0$splat\u00a0=\u00a0@{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ClassName\u00a0\u00a0\u00a0\u00a0=\u00a0\"Win32_process\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0filter\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0\"\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Property\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0\"Name\",\u00a0\"ProcessID\",\u00a0\"WorkingSetSize\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Computername\u00a0=\u00a0$Computername\n\u00a0\u00a0\u00a0\u00a0}\n}\u00a0-Process\u00a0{\n\u00a0\u00a0\u00a0\u00a0<em>#update\u00a0the\u00a0filter<\/em>\n\u00a0\u00a0\u00a0\u00a0$splat.filter\u00a0=\u00a0\"processid=$($_.processid)\"\n\u00a0\u00a0\u00a0\u00a0$proc\u00a0=\u00a0Get-CimInstance\u00a0@splat\n\u00a0\u00a0\u00a0\u00a0[pscustomobject]@{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0PSTypeName\u00a0\u00a0\u00a0\u00a0=\u00a0\"svcMemoryDetail\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ServiceName\u00a0\u00a0\u00a0=\u00a0$_.Name\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ProcessName\u00a0\u00a0\u00a0=\u00a0$proc.Name\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ProcessID\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$proc.ProcessId\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0TotalWS\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$proc.WorkingSetSize\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0PctUsedMemory\u00a0=\u00a0[math]::Round(($proc.WorkingSetSize\u00a0\/\u00a0$inUseMemory)\u00a0*\u00a0100,\u00a04)\n\u00a0\u00a0\u00a0\u00a0}\n}\u00a0|\u00a0Sort-Object\u00a0PctUsedMemory\u00a0-Descending\u00a0|\u00a0Select-Object\u00a0-First\u00a010\u00a0|\u00a0Format-Table<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/service-ws.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"252\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/service-ws-1024x252.png\" alt=\"\" class=\"wp-image-8152\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/service-ws-1024x252.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/service-ws-300x74.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/service-ws-768x189.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/service-ws-1536x379.png 1536w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/service-ws-2048x505.png 2048w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/service-ws-850x210.png 850w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>If I were to continue and create a function around this, I'd also create a custom format.ps1xml file to present a pretty table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrap-Up<\/h2>\n\n\n\n<p>If you've been following me for any length of time you should recognize a familiar workflow.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Write code that runs interactively at a prompt.<\/li><li>Create a function wrapped around the code that writes an object to the pipeline.<\/li><li>Add custom type and format updates to make it easy to work with command results.<\/li><\/ul>\n\n\n\n<p>If you use variables in your console code, like I did for $Computername, it doesn't take much effort to use them as function parameters.<\/p>\n\n\n\n<p>There's admittedly a lot going on in this post, so don't be shy if you have questions or need a little clarity. Most likely someone else does as well. Otherwise, I hope you'll try some of the code yourself to see how it works. I also want you to try your hand at the Iron Scripter challenges. There are tests for all levels and there is no deadline. <\/p>\n\n\n\n<p>Good luck.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I hope you tried your hand at this Iron Scripter PowerShell challenge on reporting memory usage. The basic challenge was to find the total percent of working set memory that a specific process or service is using. Here&#8217;s how I approached it, with my usual disclaimer that my solution is not the only or nor&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"New on the blog: Solving the Iron Scripter #PowerShell Memory Challenge","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,8],"tags":[334,534,540],"class_list":["post-8143","post","type-post","status-publish","format-standard","hentry","category-powershell","category-scripting","tag-performance-counter","tag-powershell","tag-scripting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Solving the PowerShell Memory Challenge &#8226; The Lonely Administrator<\/title>\n<meta name=\"description\" content=\"I share my code and ideas on solving an Iron Scripter PowerShell challenge to display the memory values for processes and services.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/8143\/solving-the-powershell-memory-challenge\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Solving the PowerShell Memory Challenge &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"I share my code and ideas on solving an Iron Scripter PowerShell challenge to display the memory values for processes and services.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/8143\/solving-the-powershell-memory-challenge\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-08T15:31:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-02-09T23:37:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-1024x355.png\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8143\\\/solving-the-powershell-memory-challenge\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8143\\\/solving-the-powershell-memory-challenge\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Solving the PowerShell Memory Challenge\",\"datePublished\":\"2021-02-08T15:31:04+00:00\",\"dateModified\":\"2021-02-09T23:37:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8143\\\/solving-the-powershell-memory-challenge\\\/\"},\"wordCount\":934,\"commentCount\":6,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8143\\\/solving-the-powershell-memory-challenge\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/get-wspct-1024x355.png\",\"keywords\":[\"Performance counter\",\"PowerShell\",\"Scripting\"],\"articleSection\":[\"PowerShell\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8143\\\/solving-the-powershell-memory-challenge\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8143\\\/solving-the-powershell-memory-challenge\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8143\\\/solving-the-powershell-memory-challenge\\\/\",\"name\":\"Solving the PowerShell Memory Challenge &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8143\\\/solving-the-powershell-memory-challenge\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8143\\\/solving-the-powershell-memory-challenge\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/get-wspct-1024x355.png\",\"datePublished\":\"2021-02-08T15:31:04+00:00\",\"dateModified\":\"2021-02-09T23:37:09+00:00\",\"description\":\"I share my code and ideas on solving an Iron Scripter PowerShell challenge to display the memory values for processes and services.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8143\\\/solving-the-powershell-memory-challenge\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8143\\\/solving-the-powershell-memory-challenge\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8143\\\/solving-the-powershell-memory-challenge\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/get-wspct.png\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/get-wspct.png\",\"width\":1988,\"height\":689},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8143\\\/solving-the-powershell-memory-challenge\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Solving the PowerShell Memory Challenge\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Solving the PowerShell Memory Challenge &#8226; The Lonely Administrator","description":"I share my code and ideas on solving an Iron Scripter PowerShell challenge to display the memory values for processes and services.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8143\/solving-the-powershell-memory-challenge\/","og_locale":"en_US","og_type":"article","og_title":"Solving the PowerShell Memory Challenge &#8226; The Lonely Administrator","og_description":"I share my code and ideas on solving an Iron Scripter PowerShell challenge to display the memory values for processes and services.","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8143\/solving-the-powershell-memory-challenge\/","og_site_name":"The Lonely Administrator","article_published_time":"2021-02-08T15:31:04+00:00","article_modified_time":"2021-02-09T23:37:09+00:00","og_image":[{"url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-1024x355.png","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8143\/solving-the-powershell-memory-challenge\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8143\/solving-the-powershell-memory-challenge\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Solving the PowerShell Memory Challenge","datePublished":"2021-02-08T15:31:04+00:00","dateModified":"2021-02-09T23:37:09+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8143\/solving-the-powershell-memory-challenge\/"},"wordCount":934,"commentCount":6,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8143\/solving-the-powershell-memory-challenge\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-1024x355.png","keywords":["Performance counter","PowerShell","Scripting"],"articleSection":["PowerShell","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/8143\/solving-the-powershell-memory-challenge\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8143\/solving-the-powershell-memory-challenge\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8143\/solving-the-powershell-memory-challenge\/","name":"Solving the PowerShell Memory Challenge &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8143\/solving-the-powershell-memory-challenge\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8143\/solving-the-powershell-memory-challenge\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct-1024x355.png","datePublished":"2021-02-08T15:31:04+00:00","dateModified":"2021-02-09T23:37:09+00:00","description":"I share my code and ideas on solving an Iron Scripter PowerShell challenge to display the memory values for processes and services.","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8143\/solving-the-powershell-memory-challenge\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/8143\/solving-the-powershell-memory-challenge\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8143\/solving-the-powershell-memory-challenge\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct.png","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/get-wspct.png","width":1988,"height":689},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8143\/solving-the-powershell-memory-challenge\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"Solving the PowerShell Memory Challenge"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":3019,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3019\/powershell-scripting-games-2013-impressions\/","url_meta":{"origin":8143,"position":0},"title":"PowerShell Scripting Games 2013 Impressions","author":"Jeffery Hicks","date":"May 13, 2013","format":false,"excerpt":"Now that the PowerShell Scripting Games for 2013 are well underway, I thought I'd share my thoughts and impressions on what I've seen. I'm very impressed with the number of entries and generally the quality is pretty good. But as a judge I see repeated items that bear comment. These\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/scriptinggames.org\/games-logo.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3661,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3661\/creating-cim-scripts-without-scripting\/","url_meta":{"origin":8143,"position":1},"title":"Creating CIM Scripts without Scripting","author":"Jeffery Hicks","date":"January 29, 2014","format":false,"excerpt":"When Windows 8 and Windows Server 2012 came out, along with PowerShell 3.0, we got our hands on some terrific technology in the form of the CIM cmdlets. Actually, we got much more than people realize. One of the reasons there was a big bump in the number of shipping\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4959,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4959\/the-power-of-custom-properties\/","url_meta":{"origin":8143,"position":2},"title":"The Power of Custom Properties","author":"Jeffery Hicks","date":"March 1, 2016","format":false,"excerpt":"The other day fellow PowerShell MVP Adam Bertram published an article about using custom properties with Select-Object. It is a good article in that it gets you thinking about PowerShell in terms of objects and not simple text. But I want to take Adam's article as a jumping off point\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"image","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2016\/03\/image_thumb.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2016\/03\/image_thumb.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2016\/03\/image_thumb.png?resize=525%2C300 1.5x"},"classes":[]},{"id":2935,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2935\/get-ciminstance-from-powershell-2-0\/","url_meta":{"origin":8143,"position":3},"title":"Get CIMInstance from PowerShell 2.0","author":"Jeffery Hicks","date":"April 10, 2013","format":false,"excerpt":"I love the new CIM cmdlets in PowerShell 3.0. Querying WMI is a little faster because the CIM cmdlets query WMI using the WSMAN protocol instead of DCOM. The catch is that remote computers must be running PowerShell 3 which includes the latest version of the WSMAN protocol and the\u2026","rel":"","context":"In &quot;Powershell 3.0&quot;","block_context":{"text":"Powershell 3.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-3-0\/"},"img":{"alt_text":"get-ciminstance-error","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/04\/get-ciminstance-error-300x145.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":5833,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/5833\/new-powershell-projects-published\/","url_meta":{"origin":8143,"position":4},"title":"New PowerShell Projects Published","author":"Jeffery Hicks","date":"December 18, 2017","format":false,"excerpt":"Last week I published a few of the recent PowerShell modules I've been working on to the PowerShell Gallery. These projects had been in a beta phase while I worked out some last minute features. I was also waiting to see if there were any issues reported by you that\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2017\/12\/image_thumb-6.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2017\/12\/image_thumb-6.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2017\/12\/image_thumb-6.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2342,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2342\/query-local-administrators-with-cim\/","url_meta":{"origin":8143,"position":5},"title":"Query Local Administrators with CIM","author":"Jeffery Hicks","date":"May 24, 2012","format":false,"excerpt":"Yesterday I posted an article on listing members of the local administrators group with PowerShell and Get-WmiObject. PowerShell 3.0 offers an additional way using the CIM cmdlets. The CIM cmdlets query the same WMI information, except instead of using the traditional RPC\/DCOM connection, these cmdlets utilize PowerShell's remoting endpoint so\u2026","rel":"","context":"In &quot;Powershell 3.0&quot;","block_context":{"text":"Powershell 3.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-3-0\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/05\/talkbubble-v3-150x150.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=8143"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8143\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=8143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=8143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=8143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}