├── MemoryTools.psd1 ├── MemoryTools.psm1 ├── README.md ├── Tests ├── MemoryTools.tests.ps1 └── test-runner.ps1 ├── assets └── show-memoryusage.png ├── changelog.md ├── docs ├── Get-MemoryPerformance.md ├── Get-MemoryUsage.md ├── Get-PhysicalMemory.md ├── Get-ProcessMemory.md ├── Get-TopProcessMemory.md ├── Show-MemoryUsage.md └── Test-MemoryUsage.md ├── en-us └── MemoryTools-help.xml ├── formats ├── MyMemoryUsage.format.ps1xml ├── myProcessMemory.format.ps1xml ├── physicalMemoryUnit.format.ps1xml └── topProcessMemoryUnit.format.ps1xml ├── functions ├── Get-MemoryPerformance.ps1 ├── Get-MemoryUsage.ps1 ├── Get-PhysicalMemory.ps1 ├── Get-ProcessMemory.ps1 ├── Get-TopProcessMemory.ps1 ├── Show-MemoryUsage.ps1 └── Test-MemoryUsage.ps1 ├── license.txt └── pre-release-changelog.md /MemoryTools.psd1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdhitsolutions/MemoryTools/e58f0d0950be05f56880512912eb2165a03d5ce7/MemoryTools.psd1 -------------------------------------------------------------------------------- /MemoryTools.psm1: -------------------------------------------------------------------------------- 1 | #import module variables and aliases 2 | (Get-Childitem -Path $PSScriptRoot\functions\*.ps1).Foreach({. $_.FullName}) 3 | 4 | #global settings for MemoryTools commands 5 | 6 | <# 7 | These are variables that will be used with Get-MemoryUsage 8 | to determine the status. The value is a percentage of free 9 | physical memory available. 10 | 11 | Any detected value less than $MemoryToolsWarning will be 12 | classified as Critical. 13 | 14 | You can change these variables at any time during your session. 15 | Or modify the file for permanent changes. You will need to 16 | re-import the module for the changes to take effect. 17 | #> 18 | 19 | [ValidateRange(1, 100)][int32]$Global:MemoryToolsOK = 45 20 | [ValidateRange(1, 100)][int32]$Global:MemoryToolsWarning = 15 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MemoryTools 2 | 3 | [![PSGallery Version](https://img.shields.io/powershellgallery/v/MemoryTools.png?style=for-the-badge&logo=powershell&label=PowerShell%20Gallery)](https://www.powershellgallery.com/packages/MemoryTools/) [![PSGallery Downloads](https://img.shields.io/powershellgallery/dt/MemoryTools.png?style=for-the-badge&label=Downloads)](https://www.powershellgallery.com/packages/MemoryTools/) 4 | 5 | This module contains a set of PowerShell functions for reporting on computer memory utilization and configuration. The commands use `Get-CimInstance`, so PowerShell 3.0 or later must be installed on remote computers, and they need to be configured for PowerShell remoting. 6 | 7 | The project was first described on my [blog](http://bit.ly/1Tooj3Q). 8 | 9 | Install the latest version of this module from the PowerShell Gallery. It should work in Windows PowerShell 5.1 and PowerShell 7 on a Windows platform. 10 | 11 | ```powershell 12 | Install-Module MemoryTools [-scope CurrentUser] 13 | ``` 14 | 15 | ## [Get-MemoryUsage](docs/Get-MemoryUsage.md) 16 | 17 | This command will write a custom memory utilization object to the pipeline that indicates the current memory state. 18 | 19 | ```powershell 20 | PS C:\> Get-MemoryUsage 21 | 22 | Computername Status PctFree FreeGB TotalGB 23 | ------------ ------ ------- ------ ------- 24 | PROSPERO OK 73.5 46.83 64 25 | ``` 26 | 27 | ## [Show-MemoryUsage](docs/Show-MemoryUsage.md) 28 | 29 | This command will also get the same information as Get-MemoryUsage but will display it with colorized output. 30 | 31 | ![Alt Colorized Memory Usage](assets/show-memoryusage.png) 32 | 33 | ## [Test-MemoryUsage](docs/Test-MemoryUsage.md) 34 | 35 | This command can be used to test if memory utilization meets some criteria. There are several parameter sets for different tests. All of them can be used with `-Quiet` to return a simple Boolean value. 36 | 37 | ### Percent Free 38 | 39 | The default behavior is to see if there is at least 50% free memory, but you can specify a different value. 40 | 41 | ```powershell 42 | PS C:\> Test-MemoryUsage 43 | 44 | Computername PctFree Test 45 | ------------ ------- ---- 46 | PROSPERO 73.23 True 47 | 48 | ``` 49 | 50 | ### FreeGB 51 | 52 | Test if there is at least X amount of free memory available. 53 | 54 | ```powershell 55 | PS C:\> Test-MemoryUsage -freegb 8 56 | 57 | Computername FreeGB Test 58 | ------------ ------ ---- 59 | PROSPERO 46.66 True 60 | 61 | ``` 62 | 63 | ### TotalGB 64 | 65 | Test if the computer has at least X amount of total memory. 66 | 67 | ```powershell 68 | PS C:\> Test-MemoryUsage -TotalGB 16 69 | 70 | Computername TotalGB Test 71 | ------------ ------- ---- 72 | PROSPERO 64 True 73 | 74 | ``` 75 | 76 | ## UsedGB 77 | 78 | Test if the computer is using X amount of memory or greater. 79 | 80 | ```powershell 81 | PS C:\> Test-MemoryUsage -UsedGB 4 82 | 83 | Computername UsedGB Test 84 | ------------ ------ ---- 85 | PROSPERO 17.37 True 86 | 87 | ``` 88 | 89 | ## [Get-MemoryPerformance](docs/Get-MemoryPerformance.md) 90 | 91 | This command will query memory performance counters. 92 | 93 | ```powershell 94 | PS C:\> Get-MemoryPerformance 95 | 96 | AvailableBytes : 36180754432 97 | AvailableKBytes : 35332768 98 | AvailableMBytes : 34504 99 | CacheBytes : 436342784 100 | CacheBytesPeak : 588001280 101 | CacheFaultsPersec : 0 102 | CommitLimit : 78617718784 103 | CommittedBytes : 40527859712 104 | DemandZeroFaultsPersec : 251 105 | FreeAndZeroPageListBytes : 20220755968 106 | FreeSystemPageTableEntries : 12448913 107 | LongTermAverageStandbyCacheLifetimes : 14400 108 | ModifiedPageListBytes : 1235890176 109 | PageFaultsPersec : 232 110 | PageReadsPersec : 0 111 | PagesInputPersec : 0 112 | PagesOutputPersec : 0 113 | PagesPersec : 0 114 | PageWritesPersec : 0 115 | PercentCommittedBytesInUse : 51 116 | PoolNonpagedAllocs : 0 117 | PoolNonpagedBytes : 1043083264 118 | PoolPagedAllocs : 0 119 | PoolPagedBytes : 858091520 120 | PoolPagedResidentBytes : 802910208 121 | StandbyCacheCoreBytes : 128360448 122 | StandbyCacheNormalPriorityBytes : 6915219456 123 | StandbyCacheReserveBytes : 8916418560 124 | SystemCacheResidentBytes : 436342784 125 | SystemCodeResidentBytes : 8192 126 | SystemCodeTotalBytes : 8192 127 | SystemDriverResidentBytes : 15392768 128 | SystemDriverTotalBytes : 40984576 129 | TransitionFaultsPersec : 11 130 | TransitionPagesRePurposedPersec : 0 131 | WriteCopiesPersec : 0 132 | DateTime : 11/11/2020 5:56:08 PM 133 | ComputerName : PROSPERO 134 | ``` 135 | 136 | Or you might use it like this: 137 | 138 | ```powershell 139 | PS C:\> Get-CimSession | Get-MemoryPerformance | Select-Object Computername,CommittedBytes,AvailableMBytes,CacheBytes 140 | 141 | ComputerName CommittedBytes AvailableMBytes CacheBytes 142 | ------------ -------------- --------------- ---------- 143 | thinkp1 4630245376 29007 139804672 144 | win10 1045913600 989 69541888 145 | srv1 598110208 592 22425600 146 | dom1 1357385728 2913 52137984 147 | srv2 586850304 534 28585984 148 | ``` 149 | 150 | ## [Get-PhysicalMemory](docs/Get-PhysicalMemory.md) 151 | 152 | This command will query the Win32_PhysicalMemory class to get hardware details. 153 | 154 | ```powershell 155 | PS C:\> Get-PhysicalMemory -Computername thinkp1 156 | 157 | Computername: THINKP1 158 | 159 | Manufacturer CapacityGB Form ClockSpeed Voltage Location 160 | ------------ ---------- ---- ---------- ------- -------- 161 | Samsung 16 SODIMM 2667 1200 ChannelA-DIMM0 162 | Micron 16 SODIMM 2667 1200 ChannelB-DIMM0 163 | ``` 164 | 165 | ## [Get-TopProcessMemory](docs/Get-TopProcessMemory.md) 166 | 167 | This command will use `Get-CimInstance` to retrieve the top processes by *Workingset*. It will retrieve the top 5 by default. The output will include a percentage of the total memory in use and the process owner. 168 | 169 | ```powershell 170 | PS C:\> Get-TopProcessMemory 171 | 172 | Computername: PROSPERO 173 | 174 | ProcessID : 4280 175 | Name : Memory Compression 176 | WS(MB) : 1306.37 177 | PctUsed : 4.9505 178 | Owner : NT AUTHORITY\SYSTEM 179 | Commandline : 180 | 181 | ProcessID : 40800 182 | Name : pwsh.exe 183 | WS(MB) : 1250.1 184 | PctUsed : 4.7373 185 | Owner : PROSPERO\Jeff 186 | Commandline : "C:\Program Files\PowerShell\7\pwsh.exe" -nologo 187 | 188 | ProcessID : 43612 189 | Name : pwsh.exe 190 | WS(MB) : 534.37 191 | PctUsed : 2.025 192 | Owner : PROSPERO\Jeff 193 | Commandline : "C:\Program Files\PowerShell\7\pwsh.exe" -NoProfile 194 | -ExecutionPolicy Bypass -Command "Import-Module 'c:\Users\Jeff\.vs 195 | code\extensions\ms-vscode.powershell-2023.2.1\modules\PowerShellEd 196 | itorServices\PowerShellEditorServices.psd1'; Start-EditorServices 197 | -HostName 'Visual Studio Code Host' -HostProfileId 198 | 'Microsoft.VSCode' -HostVersion '2023.2.1' -AdditionalModules 199 | @('PowerShellEditorServices.VSCode') -BundledModulesPath 'c:\Users 200 | \Jeff\.vscode\extensions\ms-vscode.powershell-2023.2.1\modules' 201 | -EnableConsoleRepl -StartupBanner '' -LogLevel 'Normal' -LogPath ' 202 | c:\Users\Jeff\AppData\Roaming\Code\User\globalStorage\ms-vscode.po 203 | wershell\logs\1679663113-13d77409-f0b9-4af6-87e8-58d6de36834f16796 204 | 62816963\EditorServices.log' -SessionDetailsPath 'c:\Users\Jeff\Ap 205 | pData\Roaming\Code\User\globalStorage\ms-vscode.powershell\session 206 | s\PSES-VSCode-29600-898846.json' -FeatureFlags @() " 207 | 208 | ProcessID : 13480 209 | Name : PhoneExperienceHost.exe 210 | WS(MB) : 431.92 211 | PctUsed : 1.6368 212 | Owner : PROSPERO\Jeff 213 | Commandline : "C:\Program Files\WindowsApps\Microsoft.YourPhone_1.23022.139.0_x6 214 | 4__8wekyb3d8bbwe\PhoneExperienceHost.exe" 215 | -Restart:{F534506B-042E-4975-BF2C-806D54F0EBC7} 216 | 217 | ProcessID : 31184 218 | Name : firefox.exe 219 | WS(MB) : 349.93 220 | PctUsed : 1.3261 221 | Owner : PROSPERO\Jeff 222 | Commandline : "C:\Program Files\Mozilla Firefox\firefox.exe" -osint -url 223 | https://pester.dev/docs/commands/Should 224 | ``` 225 | 226 | ## [Get-ProcessMemory](docs/Get-ProcessMemory.md) 227 | 228 | Get a snapshot of a process' memory usage based on its workingset value. Processes are grouped by name. 229 | 230 | ```powershell 231 | PS C:\> Get-ProcessMemory | Select-Object -first 5 232 | 233 | Name Count Threads AvgMB SumMB Computername 234 | ---- ----- ------- ----- ----- ------------ 235 | firefox 17 467 142.2585 2418.3945 PROSPERO 236 | pwsh 3 101 642.0807 1926.2422 PROSPERO 237 | svchost 102 692 16.0955 1641.7461 PROSPERO 238 | Code 13 264 117.466 1527.0586 PROSPERO 239 | Memory Compression 1 54 1306.3555 1306.3555 PROSPERO 240 | ``` 241 | 242 | ## Roadmap 243 | 244 | I have plans to revise commands to take advantage of SSH remoting in PowerShell 7. It also might be nice to have a WPF-based GUI to display memory information. 245 | -------------------------------------------------------------------------------- /Tests/MemoryTools.tests.ps1: -------------------------------------------------------------------------------- 1 | #Pester Tests 2 | <# 3 | Show-MemoryUsage can't be tested because it doesn't write anything to the pipeline 4 | This has not been tested with Pester 5.x 5 | #> 6 | 7 | Import-Module $PSScriptRoot\..\MemoryTools.psd1 -Force 8 | 9 | InModuleScope MemoryTools { 10 | 11 | BeforeAll { 12 | $Script:mod = Get-Module MemoryTools 13 | #create a sample object like something imported from a CSV file 14 | #or other command 15 | $computerObject = New-Object -TypeName PSObject -Property @{ 16 | Computername = $env:computername 17 | Location = 'Chicago' 18 | OS = 'Windows Server 2016' 19 | } 20 | 21 | #test with CimSessions 22 | $testSessions = New-CimSession -ComputerName $env:computername, $env:computername 23 | } 24 | 25 | Describe 'Module' -Tags module { 26 | 27 | It 'Should have an 7 functions' { 28 | $Script:mod.ExportedFunctions.count | Should -Be 7 29 | } 30 | It 'Should have 6 aliases' { 31 | $script:mod.ExportedAliases.count | Should -Be 6 32 | } 33 | It 'Should have 4 Format ps1xml files' { 34 | $script:mod.ExportedFormatFiles.count | Should -Be 4 35 | } 36 | It "Should have an integer value for `$MemoryToolsOK" { 37 | ($MemoryToolsOK).GetType().Name | Should -Be 'Int32' 38 | } 39 | 40 | It "Should have an integer value for `$MemoryToolsWarning" { 41 | ($MemoryToolsWarning).GetType().Name | Should -Be 'Int32' 42 | } 43 | 44 | It "Should have an external help file" { 45 | "$PSScriptRoot\..\en-us\*help.xml" | Should -Exist 46 | } 47 | 48 | It "Should have an license file" { 49 | "$PSScriptRoot\..\license*" | Should -Exist 50 | } 51 | It "Should have a README file" { 52 | "$PSScriptRoot\..\readme.md" | Should -Exist 53 | } 54 | It "Should have a Changelog file" { 55 | "$PSScriptRoot\..\changelog.*" | Should -Exist 56 | } 57 | 58 | } #describe module 59 | 60 | Describe 'Get-MemoryUsage' -Tags Functions, dev { 61 | 62 | <# 63 | I am mocking the results with known values. The tested command will actually 64 | use the localhost but the result will be mocked. 65 | #> 66 | BeforeAll { 67 | Mock Get-CimInstance { 68 | $data = [PSCustomObject]@{ 69 | PSComputername = 'WinTest' 70 | FreePhysicalMemory = 4194304 71 | TotalVisibleMemorySize = 8298776 72 | Caption = 'Windows Server 2016 Standard' 73 | } 74 | Return $Data 75 | } -ParameterFilter { $ClassName -eq 'Win32_OperatingSystem' } 76 | $script:try = Get-MemoryUsage -Computername $env:computername 77 | } 78 | 79 | It 'should run with defaults and without error' { 80 | { Get-MemoryUsage -ErrorAction stop } | Should -Not -Throw 81 | } 82 | It 'Should show status as OK' { 83 | $try.status | Should -Be 'OK' 84 | } 85 | 86 | It 'Should filter on Status' { 87 | (Get-MemoryUsage -Computername $env:computername -Status 'OK' | Measure-Object).count | Should -Be 1 88 | (Get-MemoryUsage -Computername $env:computername -Status 'Critical' | Measure-Object).count | Should -Be 0 89 | } 90 | It 'Should show computername as WinTest' { 91 | $try.computername | Should -Be 'WinTest' 92 | } 93 | It 'Should show total as 8' { 94 | $try.TotalGB | Should -Be 8 95 | } 96 | It 'Should show PctFree as 51' { 97 | $try.pctFree -as [int] | Should -Be 51 98 | } 99 | It 'Should show Free as 4' { 100 | $try.FreeGB | Should -Be 4 101 | } 102 | It 'Should have a type of MyMemoryUsage' { 103 | $try.PSObject.TypeNames[0] | Should -Be 'MyMemoryUsage' 104 | } 105 | It 'Should accept pipelined input' { 106 | { 'localhost' | Get-MemoryUsage -ErrorAction stop } | Should -Not -Throw 107 | 108 | ('localhost', 'localhost', 'localhost' | Get-MemoryUsage | Measure-Object).count | Should -Be 3 109 | 110 | ($computerobject, $computerobject | Get-MemoryUsage | Measure-Object).count | Should -Be 2 111 | 112 | } 113 | 114 | It 'Should error with a bad computername' { 115 | { Get-MemoryUsage -Computername 'F00' -ErrorAction stop } | Should -Throw 116 | } 117 | 118 | It 'Should accept CIMSessions as a parameter value' { 119 | (Get-MemoryUsage -CimSession $testSessions | Measure-Object).Count | Should -Be 2 120 | } 121 | 122 | It 'Should accept CIMSessions from the pipeline' { 123 | ($testSessions | Get-MemoryUsage | Measure-Object).Count | Should -Be 2 124 | } 125 | 126 | } 127 | 128 | Describe 'Test-MemoryUsage' -Tags Functions { 129 | 130 | BeforeAll { 131 | Mock Get-CimInstance { 132 | $data = [PSCustomObject]@{ 133 | PSComputername = 'WinTest' 134 | FreePhysicalMemory = 4194304 135 | TotalVisibleMemorySize = 8298776 136 | Caption = 'Windows Server 2016 Standard' 137 | } 138 | Return $Data 139 | } -ParameterFilter { $ClassName -eq 'Win32_OperatingSystem' } 140 | 141 | } 142 | 143 | Context 'Test-MemoryUsage: Percent' { 144 | BeforeAll { 145 | $script:r = Test-MemoryUsage -Computername $env:computername 146 | } 147 | It 'Should run with defaults and without error' { 148 | { Test-MemoryUsage -ErrorAction Stop } | Should -Not -Throw 149 | } 150 | 151 | It "Should have a Test value of $True" { 152 | $r.Test | Should -Be $True 153 | } 154 | It 'Should have a value >= 50 ' { 155 | $r.PctFree | Should -BeGreaterThan 50 156 | } 157 | It 'Should accept pipelined input' { 158 | { 'localhost' | Test-MemoryUsage -ErrorAction stop -Quiet } | Should -Not -Throw 159 | 160 | ('localhost', 'localhost', 'localhost' | Test-MemoryUsage -Quiet | Measure-Object).count | Should -Be 3 161 | 162 | ($computerobject, $computerobject | Test-MemoryUsage | Measure-Object).count | Should -Be 2 163 | 164 | } 165 | It 'Should error with a bad computername' { 166 | { Test-MemoryUsage -Computername 'F00' -ErrorAction stop } | Should -Throw 167 | } 168 | 169 | } 170 | 171 | Context 'Test-MemoryUsage: Free' { 172 | BeforeAll { 173 | $script:r = Test-MemoryUsage -Computername $env:computername -FreeGB 4 174 | } 175 | 176 | It "Should have a Test value of $True" { 177 | $r.test | Should -Be $True 178 | } 179 | It 'Should have a FreeGB value of 4' { 180 | $r.FreeGB | Should -Be 4 181 | } 182 | } 183 | 184 | Context 'Test-MemoryUsage: Total' { 185 | BeforeAll { 186 | $script:r = Test-MemoryUsage -Computername $env:computername -TotalGB 8 187 | } 188 | 189 | It "Should have a Test value of $True" { 190 | $r.test | Should -Be $True 191 | } 192 | It 'Should have a TotalGB value of 8' { 193 | $r.TotalGB | Should -Be 8 194 | } 195 | } 196 | 197 | Context 'Test-MemoryUsage: Used' { 198 | BeforeAll { 199 | $script:r = Test-MemoryUsage -Computername $env:computername -UsedGB 4 200 | } 201 | 202 | It "Should have a Test value of $True" { 203 | $r.test | Should -Be $True 204 | } 205 | It 'Should have a UsedGB value of 4' { 206 | $r.UsedGB | Should -Be 4 207 | } 208 | 209 | } 210 | } 211 | 212 | Describe 'Get-MemoryPerformance' -Tags Functions { 213 | 214 | BeforeAll { 215 | $script:r = Get-MemoryPerformance 216 | 217 | } 218 | It 'Should run with defaults and without error' { 219 | { Get-MemoryPerformance -ErrorAction Stop } | Should -Not -Throw 220 | } 221 | 222 | 223 | It "Should have a computername equal to `$env:computername" { 224 | $r.computername | Should -Be $env:COMPUTERNAME 225 | } 226 | 227 | It 'should have a datetime of today' { 228 | $today = (Get-Date) 229 | $r.datetime.hour | Should -Be $today.Hour 230 | $r.datetime.Day | Should -Be $today.Day 231 | $r.datetime.month | Should -Be $today.Month 232 | } 233 | 234 | It 'should have available bytes' { 235 | $r.AvailableBytes | Should -BeGreaterThan 0 236 | } 237 | 238 | It 'Should accept pipelined input' { 239 | { 'localhost' | Get-MemoryPerformance -ErrorAction stop } | Should -Not -Throw 240 | ('localhost', 'localhost', 'localhost' | Get-MemoryPerformance | Measure-Object).count | Should -Be 3 241 | ($computerobject, $computerobject | Get-MemoryPerformance | Measure-Object).count | Should -Be 2 242 | } 243 | 244 | It 'Should error with a bad computername' { 245 | { Get-MemoryPerformance -Computername 'F00' -ErrorAction stop } | Should -Throw 246 | } 247 | 248 | } 249 | 250 | Describe 'Get-PhysicalMemory' -Tags Functions { 251 | BeforeAll { 252 | 253 | Mock Get-CimInstance { 254 | $data = @( 255 | [PSCustomObject]@{ 256 | PSComputername = 'WinTest' 257 | Manufacturer = 0420 258 | Capacity = 16GB 259 | FormFactor = 12 260 | ConfiguredClockSpeed = 2133 261 | ConfiguredVoltage = 1200 262 | DeviceLocator = 'ChannelA-DIMM0' 263 | }, 264 | [PSCustomObject]@{ 265 | PSComputername = 'WinTest' 266 | Manufacturer = 0420 267 | Capacity = 16GB 268 | FormFactor = 12 269 | ConfiguredClockSpeed = 2133 270 | ConfiguredVoltage = 1200 271 | DeviceLocator = 'ChannelA-DIMM1' 272 | }) 273 | Return $Data 274 | } -ParameterFilter { $ClassName -eq 'Win32_PhysicalMemory' } 275 | 276 | $script:r = Get-PhysicalMemory -Computername $env:computername 277 | } 278 | 279 | It 'Should run without error' { 280 | { Get-PhysicalMemory -ErrorAction Stop } | Should -Not -Throw 281 | } 282 | 283 | It 'Should return 2 objects' { 284 | $r.count | Should -Be 2 285 | } 286 | It 'Should -Be a physicalMemoryUnit object' { 287 | $r[0].PSObject.typenames[0] | Should -Be 'physicalMemoryUnit' 288 | } 289 | It 'Should have a computername of WinTest' { 290 | $r[0].Computername | Should -Be 'WinTest' 291 | } 292 | 293 | It 'Should have a form value of SODIMM' { 294 | $r[0].Form | Should -Be 'SODIMM' 295 | } 296 | 297 | It 'Should have a capacity value of 16' { 298 | $r[0].CapacityGB | Should -Be 16 299 | } 300 | It 'Should have a clock speed of 2133' { 301 | $r[0].ClockSpeed | Should -Be 2133 302 | } 303 | 304 | It 'Should accept pipelined input' { 305 | { 'localhost' | Get-PhysicalMemory -ErrorAction stop } | Should -Not -Throw 306 | 307 | ($computerobject, $computerobject | Get-PhysicalMemory | Measure-Object).count | Should -Be 4 308 | } 309 | 310 | It 'Should error with a bad computername' { 311 | { Get-PhysicalMemory -Computername 'F00' -ErrorAction stop } | Should -Throw 312 | } 313 | } 314 | 315 | Describe 'Get-TopProcessMemory' -Tags Functions { 316 | BeforeAll { 317 | $script:r = Get-TopProcessMemory -Computername $env:computername 318 | 319 | } 320 | It 'Should run without error' { 321 | { Get-TopProcessMemory -ErrorAction Stop } | Should -Not -Throw 322 | } 323 | 324 | 325 | It 'Should return 5 objects by default' { 326 | $r.count | Should -Be 5 327 | } 328 | It 'Should -Be a topProcessMemoryUnit object' { 329 | $r[0].PSObject.typenames[0] | Should -Be 'topProcessMemoryUnit' 330 | } 331 | 332 | It 'Should have an owner value' { 333 | ($r.owner | Measure-Object).Count | Should -BeGreaterThan 0 334 | } 335 | 336 | It 'Should have a computername property' { 337 | $r[0].computername | Should -Be $env:computername 338 | } 339 | 340 | It 'Should have a CreationDate property that is a [datetime]' { 341 | $r[0].CreationDate.GetType().Name | Should -Be 'datetime' 342 | } 343 | 344 | It 'Should return a specified number of processes' { 345 | (Get-TopProcessMemory -Top 7).Count | Should -Be 7 346 | } 347 | It 'Should accept multiple computernames' { 348 | $t = Get-TopProcessMemory -Computername $env:computername, $env:computername 349 | $t.count | Should -Be 10 350 | } 351 | 352 | It 'Should accept input by pipeline' { 353 | ($env:computername, $env:computername | Get-TopProcessMemory).Count | Should -Be 10 354 | } 355 | 356 | It 'Should accept input by property name via the pipeline' { 357 | ($computerobject | Get-TopProcessMemory).Count | Should -Be 5 358 | } 359 | 360 | It 'Should accept CIMSessions via the pipeline' { 361 | ($testSessions | Get-TopProcessMemory).count | Should -Be 10 362 | } 363 | } 364 | 365 | AfterAll { 366 | #clean up 367 | $testSessions | Remove-CimSession 368 | } 369 | 370 | } #in module 371 | -------------------------------------------------------------------------------- /Tests/test-runner.ps1: -------------------------------------------------------------------------------- 1 | $config = New-PesterConfiguration 2 | $config.Output.Verbosity = "Detailed" 3 | Invoke-Pester -Configuration $config -------------------------------------------------------------------------------- /assets/show-memoryusage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdhitsolutions/MemoryTools/e58f0d0950be05f56880512912eb2165a03d5ce7/assets/show-memoryusage.png -------------------------------------------------------------------------------- /changelog.md: -------------------------------------------------------------------------------- 1 | # Memory Tools Changelog 2 | 3 | ## [Unreleased] 4 | 5 | ## [1.1.0] - 2023-03-24 6 | 7 | ### Added 8 | 9 | - Added `Get-MemoryUsage`. 10 | - Added format file `myProcessMemoryUsage.format.ps1xml`. 11 | - Help updates. 12 | - Updated `README.md`. 13 | 14 | ### Changed 15 | 16 | - General code clean up. 17 | - Restructured module, separating functions to separate files. 18 | - Migrated changelog to [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) format. 19 | - Moved pre-release change log information to `pre-release-changelog.md`. 20 | 21 | ## [1.0.0] - 2020-11-11 22 | 23 | ### Added 24 | 25 | - Added a custom format file for the `physicalMemoryUnit` object type. 26 | - Added a custom format file for the `topProcessMemoryUnit` object type. 27 | - Added online help links. 28 | 29 | ### Changed 30 | 31 | - Modified `Show-MemoryUsage` to use ANSI-escape sequences instead of `Write-Host`. 32 | - Modified `Get-PhysicalMemory` to write a custom object called `physicalMemoryUnit` to the pipeline. 33 | - Modified `Get-TopProcessMemory` to write a custom object called `topProcessMemoryUnit` to the pipeline. 34 | - Updated `README.md` and `license.txt`. 35 | - Code update and cleanup for PowerShell 7 compatibility. 36 | - Modified manifest to require PowerShell 5.1 as the minimum and support Core. 37 | - Modified table layout in `MyMemoryUsage.format.ps1xml`. 38 | - Updated Pester tests. 39 | 40 | [Unreleased]: https://github.com/jdhitsolutions/MemoryTools/compare/v1.1.0..HEAD 41 | [1.1.0]: https://github.com/jdhitsolutions/MemoryTools/compare/v1.0.0..v1.1.0 42 | [1.0.0]: https://github.com/jdhitsolutions/MemoryTools/tree/v1.0.0 -------------------------------------------------------------------------------- /docs/Get-MemoryPerformance.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: MemoryTools-help.xml 3 | Module Name: MemoryTools 4 | online version: https://bit.ly/2TrAq9d 5 | schema: 2.0.0 6 | --- 7 | 8 | # Get-MemoryPerformance 9 | 10 | ## SYNOPSIS 11 | 12 | Get memory information from performance counters 13 | 14 | ## SYNTAX 15 | 16 | ### Computername 17 | 18 | ```yaml 19 | Get-MemoryPerformance [[-Computername] ] [] 20 | ``` 21 | 22 | ### Cim 23 | 24 | ```yaml 25 | Get-MemoryPerformance -CimSession [] 26 | ``` 27 | 28 | ## DESCRIPTION 29 | 30 | Use this command to simplify the process of getting data from memory-related performance counters. 31 | 32 | ## EXAMPLES 33 | 34 | ### Example 1 35 | 36 | ```powershell 37 | PS C:\> Get-MemoryPerformance 38 | 39 | 40 | 41 | AvailableBytes : 17207111680 42 | AvailableKBytes : 16803820 43 | AvailableMBytes : 16409 44 | CacheBytes : 374910976 45 | CacheBytesPeak : 627560448 46 | CacheFaultsPersec : 6419 47 | CommitLimit : 39345614848 48 | CommittedBytes : 21974437888 49 | DemandZeroFaultsPersec : 741 50 | FreeAndZeroPageListBytes : 696127488 51 | FreeSystemPageTableEntries : 12267190 52 | LongTermAverageStandbyCacheLifetimes : 14400 53 | ModifiedPageListBytes : 24715264 54 | PageFaultsPersec : 7204 55 | PageReadsPersec : 0 56 | PagesInputPersec : 0 57 | PagesOutputPersec : 0 58 | PagesPersec : 0 59 | PageWritesPersec : 0 60 | PercentCommittedBytesInUse : 55 61 | PoolNonpagedAllocs : 1182049 62 | PoolNonpagedBytes : 559136768 63 | PoolPagedAllocs : 1398128 64 | PoolPagedBytes : 831823872 65 | PoolPagedResidentBytes : 741318656 66 | StandbyCacheCoreBytes : 155684864 67 | StandbyCacheNormalPriorityBytes : 9012703232 68 | StandbyCacheReserveBytes : 7342587904 69 | SystemCacheResidentBytes : 374902784 70 | SystemCodeResidentBytes : 0 71 | SystemCodeTotalBytes : 0 72 | SystemDriverResidentBytes : 12992512 73 | SystemDriverTotalBytes : 29093888 74 | TransitionFaultsPersec : 83 75 | TransitionPagesRePurposedPersec : 0 76 | WriteCopiesPersec : 0 77 | DateTime : 11/21/2020 12:17:16 PM 78 | ComputerName : WIN10Desktop 79 | ``` 80 | 81 | Get all memory counters for the local computer. 82 | 83 | ### Example 2 84 | 85 | ```powershell 86 | PS C:\> Get-MemoryPerformance -Computername $c | Select Computername,%CommittedBytes*,AvailableMBytes,CacheBytes 87 | 88 | Computername %CommittedBytesInUse AvailableMbytes CacheBytes 89 | ------------ -------------------- --------------- ---------- 90 | CHI-HVR1 7.05788756820636 14839 99377152 91 | CHI-HVR2 16.8986961377323 13144 83001344 92 | CHI-P50 24.753204334977 47143 94998528 93 | CHI-DC04 47.8621563768474 1030 180101120 94 | Win10-ENT-01 79.914213523203 1545 96137216 95 | ``` 96 | 97 | Get selected performance data from multiple computers defined in variable c. 98 | 99 | ## PARAMETERS 100 | 101 | ### -CimSession 102 | 103 | Connect to a CimSession. 104 | 105 | ```yaml 106 | Type: CimSession[] 107 | Parameter Sets: CimInstanceSessionSet 108 | Aliases: 109 | 110 | Required: True 111 | Position: Named 112 | Default value: None 113 | Accept pipeline input: True (ByValue) 114 | Accept wildcard characters: False 115 | ``` 116 | 117 | ### -Computername 118 | 119 | Enter the name of a computer to connect to. 120 | 121 | ```yaml 122 | Type: String[] 123 | Parameter Sets: ComputernameSet 124 | Aliases: cn 125 | 126 | Required: False 127 | Position: 0 128 | Default value: None 129 | Accept pipeline input: True (ByPropertyName, ByValue) 130 | Accept wildcard characters: False 131 | ``` 132 | 133 | ### CommonParameters 134 | 135 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. 136 | For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). 137 | 138 | ## INPUTS 139 | 140 | ### System.String[] 141 | 142 | ### Microsoft.Management.Infrastructure.CimSession[] 143 | 144 | ## OUTPUTS 145 | 146 | ### System.Object 147 | 148 | ## NOTES 149 | 150 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 151 | 152 | ## RELATED LINKS 153 | 154 | [Get-Counter]() 155 | 156 | [Get-MemoryUsage](Get-MemoryUsage.md) 157 | -------------------------------------------------------------------------------- /docs/Get-MemoryUsage.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: MemoryTools-help.xml 3 | Module Name: MemoryTools 4 | online version: https://bit.ly/2TpnLDV 5 | schema: 2.0.0 6 | --- 7 | 8 | # Get-MemoryUsage 9 | 10 | ## SYNOPSIS 11 | 12 | Get a snapshot of memory usage. 13 | 14 | ## SYNTAX 15 | 16 | ### Computername (Default) 17 | 18 | ```yaml 19 | Get-MemoryUsage [[-Computername] ] [-Status ] [] 20 | ``` 21 | 22 | ### Cim 23 | 24 | ```yaml 25 | Get-MemoryUsage [-Status ] -CimSession [] 26 | ``` 27 | 28 | ## DESCRIPTION 29 | 30 | This command will write a custom memory utilization object to the pipeline that indicates the current memory state. The criteria for what is normal or not is based on the $MemoryToolsOK and $MemoryToolsWarning global variable values. You can modify these values from the console. 31 | 32 | ## EXAMPLES 33 | 34 | ### Example 1 35 | 36 | ```powershell 37 | PS C:\> Get-MemoryUsage -computername Thinkp1 38 | 39 | Computername Status PctFree FreeGB TotalGB 40 | ------------ ------ ------- ------ ------- 41 | THINKP1 OK 83.66 26.39 32 42 | ``` 43 | 44 | ### Example 2 45 | 46 | ```powershell 47 | PS C:\> Get-MemoryUsage dom1,srv1,srv2,win10 -Status Warning 48 | 49 | Computername Status PctFree FreeGB TotalGB 50 | ------------ ------ ------- ------ ------- 51 | WIN10 Warning 36.57 1.17 3 52 | ``` 53 | 54 | Get usage from several computers but filter and only display those with a Warning. 55 | 56 | ### Example 3 57 | 58 | ```powershell 59 | PS C:\> Get-MemoryUsage dom1,srv1,srv2,win10 | Format-Table -view show 60 | ``` 61 | 62 | Display a similar table as the previous example, but highlight using ANSI escape sequences. 63 | 64 | ## PARAMETERS 65 | 66 | ### -CimSession 67 | 68 | Connect using an existing CIMSession 69 | 70 | ```yaml 71 | Type: CimSession[] 72 | Parameter Sets: CimInstanceSessionSet 73 | Aliases: 74 | 75 | Required: True 76 | Position: Named 77 | Default value: None 78 | Accept pipeline input: True (ByValue) 79 | Accept wildcard characters: False 80 | ``` 81 | 82 | ### -Computername 83 | 84 | Specify the name of the computer or computers to query. 85 | 86 | ```yaml 87 | Type: String[] 88 | Parameter Sets: ComputernameSet 89 | Aliases: cn 90 | 91 | Required: False 92 | Position: 0 93 | Default value: None 94 | Accept pipeline input: True (ByPropertyName, ByValue) 95 | Accept wildcard characters: False 96 | ``` 97 | 98 | ### -Status 99 | 100 | Filter on a given status. 101 | 102 | ```yaml 103 | Type: String 104 | Parameter Sets: (All) 105 | Aliases: 106 | Accepted values: All, OK, Warning, Critical 107 | 108 | Required: False 109 | Position: Named 110 | Default value: All 111 | Accept pipeline input: False 112 | Accept wildcard characters: False 113 | ``` 114 | 115 | ### CommonParameters 116 | 117 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. 118 | For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). 119 | 120 | ## INPUTS 121 | 122 | ### System.String[] 123 | 124 | ### Microsoft.Management.Infrastructure.CimSession[] 125 | 126 | ## OUTPUTS 127 | 128 | ### MyMemoryUsage 129 | 130 | ## NOTES 131 | 132 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 133 | 134 | ## RELATED LINKS 135 | 136 | [Show-MemoryUsage](Show-MemoryUsage.md) 137 | 138 | [Test-MemoryUsage](Test-MemoryUsage.md) 139 | 140 | [Get-TopProcessMemory](Get-TopProcessMemory.md) 141 | -------------------------------------------------------------------------------- /docs/Get-PhysicalMemory.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: MemoryTools-help.xml 3 | Module Name: MemoryTools 4 | online version: https://bit.ly/2HAmHKD 5 | schema: 2.0.0 6 | --- 7 | 8 | # Get-PhysicalMemory 9 | 10 | ## SYNOPSIS 11 | 12 | Get physical memory details. 13 | 14 | ## SYNTAX 15 | 16 | ### ComputerNameSet (Default) 17 | 18 | ```yaml 19 | Get-PhysicalMemory [] 20 | ``` 21 | 22 | ### Computername 23 | 24 | ```yaml 25 | Get-PhysicalMemory [[-Computername] ] [] 26 | ``` 27 | 28 | ### Cim 29 | 30 | ```yaml 31 | Get-PhysicalMemory -CimSession [] 32 | ``` 33 | 34 | ## DESCRIPTION 35 | 36 | This command will query the Win32_PhysicalMemory class to get hardware details about installed memory. The command writes a custom object to the pipeline. 37 | 38 | ## EXAMPLES 39 | 40 | ### Example 1 41 | 42 | ```powershell 43 | PS C:\> Get-PhysicalMemory 44 | 45 | Computername: PROSPERO 46 | 47 | Manufacturer CapacityGB Form ClockSpeed Voltage Location 48 | ------------ ---------- ---- ---------- ------- -------- 49 | Crucial 32 SODIMM 2666 1200 ChannelA-DIMM0 50 | Crucial 32 SODIMM 2666 1200 ChannelB-DIMM0 51 | ``` 52 | 53 | ### Example 2 54 | 55 | ```powershell 56 | PS C:\> Get-Physicalmemory -Computername thinkp1,prospero 57 | 58 | Computername: THINKP1 59 | 60 | Manufacturer CapacityGB Form ClockSpeed Voltage Location 61 | ------------ ---------- ---- ---------- ------- -------- 62 | Samsung 16 SODIMM 2667 1200 ChannelA-DIMM0 63 | Micron 16 SODIMM 2667 1200 ChannelB-DIMM0 64 | 65 | Computername: PROSPERO 66 | 67 | Manufacturer CapacityGB Form ClockSpeed Voltage Location 68 | ------------ ---------- ---- ---------- ------- -------- 69 | Crucial 32 SODIMM 2666 1200 ChannelA-DIMM0 70 | Crucial 32 SODIMM 2666 1200 ChannelB-DIMM0 71 | ``` 72 | 73 | ### Example 3 74 | 75 | ```powershell 76 | PS C:\> Get-Cimsession | Get-PhysicalMemory 77 | 78 | Computername: WIN10 79 | 80 | Manufacturer CapacityGB Form ClockSpeed Voltage Location 81 | ------------ ---------- ---- ---------- ------- -------- 82 | Microsoft Corporation 2 Unknown 0 0 M0001 83 | 84 | Computername: SRV1 85 | 86 | Manufacturer CapacityGB Form ClockSpeed Voltage Location 87 | ------------ ---------- ---- ---------- ------- -------- 88 | Microsoft Corporation 2 Unknown 0 0 M0001 89 | 90 | Computername: DOM1 91 | 92 | Manufacturer CapacityGB Form ClockSpeed Voltage Location 93 | ------------ ---------- ---- ---------- ------- -------- 94 | Microsoft Corporation 3.875 Unknown 0 0 M0001 95 | Microsoft Corporation 4.125 Unknown 0 0 M0002 96 | ``` 97 | 98 | Pipe existing CIMSessions to the command. This example querying Hyper-V virtual machines so the results are a bit different. 99 | 100 | ## PARAMETERS 101 | 102 | ### -CimSession 103 | 104 | Connect via an existing CimSession. 105 | 106 | ```yaml 107 | Type: CimSession[] 108 | Parameter Sets: CimInstanceSessionSet 109 | Aliases: 110 | 111 | Required: True 112 | Position: Named 113 | Default value: None 114 | Accept pipeline input: True (ByValue) 115 | Accept wildcard characters: False 116 | ``` 117 | 118 | ### -Computername 119 | 120 | Specify the name of a computer or computers to query. 121 | 122 | ```yaml 123 | Type: String[] 124 | Parameter Sets: ComputernameSet 125 | Aliases: cn 126 | 127 | Required: False 128 | Position: 0 129 | Default value: None 130 | Accept pipeline input: True (ByPropertyName, ByValue) 131 | Accept wildcard characters: False 132 | ``` 133 | 134 | ### CommonParameters 135 | 136 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. 137 | For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). 138 | 139 | ## INPUTS 140 | 141 | ### System.String[] 142 | 143 | ### Microsoft.Management.Infrastructure.CimSession[] 144 | 145 | ## OUTPUTS 146 | 147 | ### physicalMemoryUnit 148 | 149 | ## NOTES 150 | 151 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 152 | 153 | ## RELATED LINKS 154 | -------------------------------------------------------------------------------- /docs/Get-ProcessMemory.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: MemoryTools-help.xml 3 | Module Name: MemoryTools 4 | online version: 5 | schema: 2.0.0 6 | --- 7 | 8 | # Get-ProcessMemory 9 | 10 | ## SYNOPSIS 11 | 12 | Get a snapshot of a process' memory usage. 13 | 14 | ## SYNTAX 15 | 16 | ```yaml 17 | Get-ProcessMemory [[-Name] ] [-Computername ] 18 | [-Credential ] [-ThrottleLimit ] 19 | [-Authentication ] [] 20 | ``` 21 | 22 | ## DESCRIPTION 23 | 24 | Get a snapshot of a process' memory usage based on its workingset value. 25 | You can get the same information using Get-Process or by querying the Win32_Process WMI class with Get-CimInstance. 26 | This command uses Invoke-Command to gather the information remotely. 27 | Many of the parameters are from that cmdlet. 28 | 29 | ## EXAMPLES 30 | 31 | ### EXAMPLE 1 32 | 33 | ```powershell 34 | PS C:\> Get-ProcessMemory code,powershell* 35 | 36 | Name Count Threads AvgMB SumMB Computername 37 | ---- ----- ------- ----- ----- ------------ 38 | Code 9 154 112.8199 1015.3789 BOVINE320 39 | powershell 3 77 179.1367 537.4102 BOVINE320 40 | powershell_ise 1 21 242.0586 242.0586 BOVINE320 41 | ``` 42 | 43 | The default output displays the process name, the total process count, the total number of threads, the average workingset value per process in MB and the total workingset size of all processes also formatted in MB. 44 | 45 | ### EXAMPLE 2 46 | 47 | ```powershell 48 | PS C:\> Get-ProcessMemory -computername srv1,srv2,dom1 -Name lsass -cred company\artd 49 | 50 | Name Count Threads AvgMB SumMB Computername 51 | ---- ----- ------- ----- ----- ------------ 52 | lsass 1 30 60.1719 60.1719 DOM1 53 | lsass 1 7 10.8594 10.8594 SRV1 54 | lsass 1 7 9.6953 9.6953 SRV2 55 | ``` 56 | 57 | ### EXAMPLE 3 58 | 59 | ```powershell 60 | PS C:\> Get-ProcessMemory *ActiveDirectory* -Computername dom1 | Select-Object * 61 | 62 | Name : Microsoft.ActiveDirectory.WebServices 63 | Count : 1 64 | Threads : 10 65 | Average : 46940160 66 | Sum : 46940160 67 | Computername : DOM1 68 | ``` 69 | 70 | This example uses a wildcard for the process name because the domain controller only has one related process. 71 | The output shows the raw values. 72 | 73 | ### EXAMPLE 4 74 | 75 | ```powershell 76 | PS C:\> Get-ProcessMemory *edge*,firefox,chrome | sort sum -Descending 77 | 78 | Name Count Threads AvgMB SumMB Computername 79 | ---- ----- ------- ----- ----- ------------ 80 | firefox 6 189 180.4134 1082.4805 BOVINE320 81 | chrome 8 124 67.1377 537.1016 BOVINE320 82 | MicrosoftEdgeCP 4 171 66.1846 264.7383 BOVINE320 83 | MicrosoftEdge 1 37 70.1367 70.1367 BOVINE320 84 | MicrosoftEdgeSH 1 10 8.2734 8.2734 BOVINE320 85 | ``` 86 | 87 | Get browser processes and sort on the underlying SUM property in descending order. 88 | 89 | ## PARAMETERS 90 | 91 | ### -Name 92 | 93 | The process name. 94 | 95 | ```yaml 96 | Type: String[] 97 | Parameter Sets: (All) 98 | Aliases: 99 | 100 | Required: False 101 | Position: 1 102 | Default value: * 103 | Accept pipeline input: True (ByValue) 104 | Accept wildcard characters: False 105 | ``` 106 | 107 | ### -Computername 108 | 109 | The name of a remote computer. 110 | 111 | ```yaml 112 | Type: String[] 113 | Parameter Sets: (All) 114 | Aliases: 115 | 116 | Required: False 117 | Position: Named 118 | Default value: $env:COMPUTERNAME 119 | Accept pipeline input: False 120 | Accept wildcard characters: False 121 | ``` 122 | 123 | ### -Credential 124 | 125 | A credential for the remote computer. 126 | 127 | ```yaml 128 | Type: PSCredential 129 | Parameter Sets: (All) 130 | Aliases: 131 | 132 | Required: False 133 | Position: Named 134 | Default value: None 135 | Accept pipeline input: False 136 | Accept wildcard characters: False 137 | ``` 138 | 139 | ### -ThrottleLimit 140 | 141 | ```yaml 142 | Type: Int32 143 | Parameter Sets: (All) 144 | Aliases: 145 | 146 | Required: False 147 | Position: Named 148 | Default value: 0 149 | Accept pipeline input: False 150 | Accept wildcard characters: False 151 | ``` 152 | 153 | ### -Authentication 154 | 155 | ```yaml 156 | Type: String 157 | Parameter Sets: (All) 158 | Aliases: 159 | 160 | Required: False 161 | Position: Named 162 | Default value: Default 163 | Accept pipeline input: False 164 | Accept wildcard characters: False 165 | ``` 166 | 167 | ### CommonParameters 168 | 169 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). 170 | 171 | ## INPUTS 172 | 173 | ### [string[]] 174 | 175 | ## OUTPUTS 176 | 177 | ### myProcessMemory 178 | 179 | ## NOTES 180 | 181 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 182 | 183 | ## RELATED LINKS 184 | 185 | [Get-Process]() 186 | [Get-TopProcessMemory](Get-TopProcessMemory.md) 187 | -------------------------------------------------------------------------------- /docs/Get-TopProcessMemory.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: MemoryTools-help.xml 3 | Module Name: MemoryTools 4 | online version: https://bit.ly/34y1oCF 5 | schema: 2.0.0 6 | --- 7 | 8 | # Get-TopProcessMemory 9 | 10 | ## SYNOPSIS 11 | 12 | Display processes using the most memory based on WorkingSet. 13 | 14 | ## SYNTAX 15 | 16 | ### ComputerName (Default) 17 | 18 | ```yaml 19 | Get-TopProcessMemory [[-Computername] ] [-Top ] [] 20 | ``` 21 | 22 | ### Cim 23 | 24 | ```yaml 25 | Get-TopProcessMemory -CimSession [-Top ] [] 26 | ``` 27 | 28 | ## DESCRIPTION 29 | 30 | This command will use Get-CimInstance to retrieve the top processes by WorkingSet. It will retrieve the top 5 by default. The output will include a percentage of total in-use memory the process is using as well as the process owner. 31 | 32 | ## EXAMPLES 33 | 34 | ### Example 1 35 | 36 | ```powershell 37 | PS C:\> Get-TopProcessMemory -top 1 38 | 39 | Computername: PROSPERO 40 | 41 | ProcessID : 3060 42 | Name : powershell.exe 43 | WS(MB) : 1455.89 44 | PctUsed : 4.8277 45 | Owner : PROSPERO\Jeff 46 | Commandline : "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Version 5.1 -s -NoLogo -NoProfile 47 | ``` 48 | 49 | Get the top process for the local computer. 50 | 51 | ### Example 2 52 | 53 | ```powershell 54 | PS C:\> Get-Cimsession | Get-TopProcessMemory | Sort Owner | Format-Table -GroupBy Owner -Property ProcessID,Name,WS,PctUsed 55 | 56 | Owner: COMPANY\ArtD 57 | 58 | Computername ProcessID Name WS PctUsed 59 | ------------ --------- ---- -- ------- 60 | WIN10 3520 powershell.exe 153464832 14.2925 61 | 62 | Owner: NT AUTHORITY\SYSTEM 63 | 64 | Computername ProcessID Name WS PctUsed 65 | ------------ --------- ---- -- ------- 66 | WIN10 2396 MsMpEng.exe 164093952 15.2824 67 | WIN10 1400 svchost.exe 65187840 6.0711 68 | WIN10 984 LogonUI.exe 55332864 5.1533 69 | WIN10 1056 svchost.exe 47890432 4.4601 70 | ``` 71 | 72 | Use an existing CIMSession and get the top 5 processes. The output is sorted by the Owner property and formatted as a table. 73 | 74 | ## PARAMETERS 75 | 76 | ### -CimSession 77 | 78 | Connect via an existing CimSession. 79 | 80 | ```yaml 81 | Type: CimSession[] 82 | Parameter Sets: CimInstanceSessionSet 83 | Aliases: 84 | 85 | Required: True 86 | Position: Named 87 | Default value: None 88 | Accept pipeline input: True (ByValue) 89 | Accept wildcard characters: False 90 | ``` 91 | 92 | ### -Computername 93 | 94 | The name of a computer or computers to query. 95 | 96 | ```yaml 97 | Type: String[] 98 | Parameter Sets: ComputernameSet 99 | Aliases: cn 100 | 101 | Required: False 102 | Position: 0 103 | Default value: None 104 | Accept pipeline input: True (ByPropertyName, ByValue) 105 | Accept wildcard characters: False 106 | ``` 107 | 108 | ### -Top 109 | 110 | The number of processes to retrieve. There is a maximum value of 25. 111 | 112 | ```yaml 113 | Type: Int32 114 | Parameter Sets: (All) 115 | Aliases: 116 | 117 | Required: False 118 | Position: Named 119 | Default value: 5 120 | Accept pipeline input: False 121 | Accept wildcard characters: False 122 | ``` 123 | 124 | ### CommonParameters 125 | 126 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. 127 | For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). 128 | 129 | ## INPUTS 130 | 131 | ### System.String[] 132 | 133 | ### Microsoft.Management.Infrastructure.CimSession[] 134 | 135 | ## OUTPUTS 136 | 137 | ### topProcessMemoryUnit 138 | 139 | ## NOTES 140 | 141 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 142 | 143 | ## RELATED LINKS 144 | 145 | [Get-MemoryUsage](Get-MemoryUsage.md) 146 | 147 | [Show-MemoryUsage](Show-MemoryUsage.md) 148 | -------------------------------------------------------------------------------- /docs/Show-MemoryUsage.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: MemoryTools-help.xml 3 | Module Name: MemoryTools 4 | online version: https://bit.ly/2Tp7IWy 5 | schema: 2.0.0 6 | --- 7 | 8 | # Show-MemoryUsage 9 | 10 | ## SYNOPSIS 11 | 12 | Display a colorized version of memory usage. 13 | 14 | ## SYNTAX 15 | 16 | ### Computername (Default) 17 | 18 | ```yaml 19 | Show-MemoryUsage [[-Computername] ] [] 20 | ``` 21 | 22 | ### Cim 23 | 24 | ```yaml 25 | Show-MemoryUsage -CimSession [] 26 | ``` 27 | 28 | ## DESCRIPTION 29 | 30 | This is a variation of the Get-MemoryUsage command. This version displays a colorized and formatted table using a custom view. Normal values will be displayed in green. Warnings will be displayed in yellow and Critical systems in red. The criteria for what is normal or not is based on the $MemoryToolsOK and $MemoryToolsWarning global variable values. You can modify these values from the console. 31 | 32 | ## EXAMPLES 33 | 34 | ### Example 1 35 | 36 | ```powershell 37 | PS C:\> show-memoryusage -Computername Dom1,Srv1,Srv2,Win10 38 | 39 | **************** 40 | Memory Check 41 | **************** 42 | 43 | Computername Status PctFree FreeGB TotalGB 44 | ------------ ------ ------- ------ ------- 45 | DOM1 OK 99.92 1023.2 1024 46 | SRV1 OK 99.96 1023.63 1024 47 | SRV2 OK 99.95 1023.51 1024 48 | WIN10 Warning 36.89 1.19 3 49 | ``` 50 | 51 | The line for Win10 would be displayed in Yellow. The other computers would be displayed in Green. 52 | 53 | ## PARAMETERS 54 | 55 | ### -CimSession 56 | 57 | Connect to an existing CimSession. 58 | 59 | ```yaml 60 | Type: CimSession[] 61 | Parameter Sets: CimInstanceSessionSet 62 | Aliases: 63 | 64 | Required: True 65 | Position: Named 66 | Default value: None 67 | Accept pipeline input: True (ByValue) 68 | Accept wildcard characters: False 69 | ``` 70 | 71 | ### -Computername 72 | 73 | The name of a computer or computers to query. 74 | 75 | ```yaml 76 | Type: String[] 77 | Parameter Sets: ComputernameSet 78 | Aliases: cn 79 | 80 | Required: False 81 | Position: 0 82 | Default value: None 83 | Accept pipeline input: True (ByPropertyName, ByValue) 84 | Accept wildcard characters: False 85 | ``` 86 | 87 | ### CommonParameters 88 | 89 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. 90 | For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). 91 | 92 | ## INPUTS 93 | 94 | ### System.String[] 95 | 96 | ### Microsoft.Management.Infrastructure.CimSession[] 97 | 98 | ## OUTPUTS 99 | 100 | ### This command creates a formatted table 101 | 102 | ## NOTES 103 | 104 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 105 | 106 | ## RELATED LINKS 107 | 108 | [Get-MemoryUsage](Get-MemoryUsage.md) 109 | -------------------------------------------------------------------------------- /docs/Test-MemoryUsage.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: MemoryTools-help.xml 3 | Module Name: MemoryTools 4 | online version: https://bit.ly/2TFkXml 5 | schema: 2.0.0 6 | --- 7 | 8 | # Test-MemoryUsage 9 | 10 | ## SYNOPSIS 11 | 12 | Test if a computer meets a specific memory threshold. 13 | 14 | ## SYNTAX 15 | 16 | ### PercentComputer (Default) 17 | 18 | ```yaml 19 | Test-MemoryUsage [[-Computername] ] [-PercentFree ] [-Quiet] [] 20 | ``` 21 | 22 | ### UsedComputer 23 | 24 | ```yaml 25 | Test-MemoryUsage [[-Computername] ] -UsedGB [-Quiet] [] 26 | ``` 27 | 28 | ### TotalComputer 29 | 30 | ```yaml 31 | Test-MemoryUsage [[-Computername] ] -TotalGB [-Quiet] [] 32 | ``` 33 | 34 | ### FreeComputer 35 | 36 | ```yaml 37 | Test-MemoryUsage [[-Computername] ] -FreeGB [-Quiet] [] 38 | ``` 39 | 40 | ### UsedCIM 41 | 42 | ```yaml 43 | Test-MemoryUsage -CimSession -UsedGB [-Quiet] [] 44 | ``` 45 | 46 | ### TotalCIM 47 | 48 | ```yaml 49 | Test-MemoryUsage -CimSession -TotalGB [-Quiet] [] 50 | ``` 51 | 52 | ### FreeCIM 53 | 54 | ```yaml 55 | Test-MemoryUsage -CimSession -FreeGB [-Quiet] [] 56 | ``` 57 | 58 | ### PercentCIM 59 | 60 | ```yaml 61 | Test-MemoryUsage -CimSession [-PercentFree ] [-Quiet] [] 62 | ``` 63 | 64 | ## DESCRIPTION 65 | 66 | This command can be used to test if memory utilization meets some criteria. There are several parameter sets for different tests. All of them can be used with -Quiet to return a simple Boolean value. 67 | 68 | ## EXAMPLES 69 | 70 | ### Example 1 71 | 72 | ```powershell 73 | PS C:\> Test-MemoryUsage 74 | 75 | Computername PctFree Test 76 | ------------ ------- ---- 77 | BOVINE320 50.53 True 78 | ``` 79 | 80 | Run the default test on the local computer. The default percentage threshold is 50%. 81 | 82 | ### Example 2 83 | 84 | ```powershell 85 | PS C:\> Test-MemoryUsage -FreeGB 16 86 | 87 | Computername FreeGB Test 88 | ------------ ------ ---- 89 | BOVINE320 15.93 False 90 | ``` 91 | 92 | Test if the computer has at least 16GB of free memory. 93 | 94 | ### Example 3 95 | 96 | ```powershell 97 | PS C:\> Test-MemoryUsage -computername CHI-P50 -TotalGB 32 -Quiet 98 | True 99 | ``` 100 | 101 | Test if the computer has at least 32GB of memory but only return True or False. 102 | 103 | ### Example 4 104 | 105 | ```powershell 106 | PS C:\> Test-MemoryUsage -UsedGB 20 107 | 108 | Computername UsedGB Test 109 | ------------ ------ ---- 110 | BOVINE320 15.74 False 111 | ``` 112 | 113 | Test if the computer is using at least 20GB of memory. 114 | 115 | ## PARAMETERS 116 | 117 | ### -CimSession 118 | 119 | Connect to an existing CimSession. 120 | 121 | ```yaml 122 | Type: CimSession[] 123 | Parameter Sets: UsedCIM, TotalCIM, FreeCIM, PercentCIM 124 | Aliases: 125 | 126 | Required: True 127 | Position: Named 128 | Default value: None 129 | Accept pipeline input: False 130 | Accept wildcard characters: False 131 | ``` 132 | 133 | ### -Computername 134 | 135 | Enter the name of a computer or computers to test. 136 | 137 | ```yaml 138 | Type: String[] 139 | Parameter Sets: PercentComputer, UsedComputer, TotalComputer, FreeComputer 140 | Aliases: cn 141 | 142 | Required: False 143 | Position: 0 144 | Default value: None 145 | Accept pipeline input: True (ByPropertyName, ByValue) 146 | Accept wildcard characters: False 147 | ``` 148 | 149 | ### -FreeGB 150 | 151 | Enter the minimum free memory in GB. 152 | 153 | ```yaml 154 | Type: Double 155 | Parameter Sets: FreeComputer, FreeCIM 156 | Aliases: 157 | 158 | Required: True 159 | Position: Named 160 | Default value: None 161 | Accept pipeline input: False 162 | Accept wildcard characters: False 163 | ``` 164 | 165 | ### -PercentFree 166 | 167 | Enter the minimum % free memory 168 | 169 | ```yaml 170 | Type: Int32 171 | Parameter Sets: PercentComputer, PercentCIM 172 | Aliases: 173 | 174 | Required: False 175 | Position: Named 176 | Default value: 50 177 | Accept pipeline input: False 178 | Accept wildcard characters: False 179 | ``` 180 | 181 | ### -Quiet 182 | 183 | Suppress details and return a Boolean value. 184 | 185 | ```yaml 186 | Type: SwitchParameter 187 | Parameter Sets: (All) 188 | Aliases: 189 | 190 | Required: False 191 | Position: Named 192 | Default value: None 193 | Accept pipeline input: False 194 | Accept wildcard characters: False 195 | ``` 196 | 197 | ### -TotalGB 198 | 199 | Enter the minimum total memory in GB 200 | 201 | ```yaml 202 | Type: Int32 203 | Parameter Sets: TotalComputer, TotalCIM 204 | Aliases: 205 | 206 | Required: True 207 | Position: Named 208 | Default value: None 209 | Accept pipeline input: False 210 | Accept wildcard characters: False 211 | ``` 212 | 213 | ### -UsedGB 214 | 215 | Enter the minimum amount of used memory in GB 216 | 217 | ```yaml 218 | Type: Double 219 | Parameter Sets: UsedComputer, UsedCIM 220 | Aliases: 221 | 222 | Required: True 223 | Position: Named 224 | Default value: None 225 | Accept pipeline input: False 226 | Accept wildcard characters: False 227 | ``` 228 | 229 | ### CommonParameters 230 | 231 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. 232 | For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). 233 | 234 | ## INPUTS 235 | 236 | ### System.String[] 237 | 238 | ## OUTPUTS 239 | 240 | ### System.Object 241 | 242 | ## NOTES 243 | 244 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 245 | 246 | ## RELATED LINKS 247 | 248 | [Get-MemoryUsage](Get-MemoryUsage.md) 249 | 250 | [Get-MemoryPerformance](Get-MemoryPerformance.md) 251 | 252 | [Get-TopProcessMemory](Get-TopProcessMemory.md) 253 | -------------------------------------------------------------------------------- /en-us/MemoryTools-help.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Get-MemoryPerformance 6 | Get 7 | MemoryPerformance 8 | 9 | Get memory information from performance counters 10 | 11 | 12 | 13 | Use this command to simplify the process of getting data from memory-related performance counters. 14 | 15 | 16 | 17 | Get-MemoryPerformance 18 | 19 | CimSession 20 | 21 | Connect to a CimSession. 22 | 23 | CimSession[] 24 | 25 | CimSession[] 26 | 27 | 28 | None 29 | 30 | 31 | 32 | Get-MemoryPerformance 33 | 34 | Computername 35 | 36 | Enter the name of a computer to connect to. 37 | 38 | String[] 39 | 40 | String[] 41 | 42 | 43 | None 44 | 45 | 46 | 47 | 48 | 49 | CimSession 50 | 51 | Connect to a CimSession. 52 | 53 | CimSession[] 54 | 55 | CimSession[] 56 | 57 | 58 | None 59 | 60 | 61 | Computername 62 | 63 | Enter the name of a computer to connect to. 64 | 65 | String[] 66 | 67 | String[] 68 | 69 | 70 | None 71 | 72 | 73 | 74 | 75 | 76 | System.String[] 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | Microsoft.Management.Infrastructure.CimSession[] 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | System.Object 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 104 | 105 | 106 | 107 | 108 | -------------------------- Example 1 -------------------------- 109 | PS C:\> Get-MemoryPerformance 110 | 111 | 112 | 113 | AvailableBytes : 17207111680 114 | AvailableKBytes : 16803820 115 | AvailableMBytes : 16409 116 | CacheBytes : 374910976 117 | CacheBytesPeak : 627560448 118 | CacheFaultsPersec : 6419 119 | CommitLimit : 39345614848 120 | CommittedBytes : 21974437888 121 | DemandZeroFaultsPersec : 741 122 | FreeAndZeroPageListBytes : 696127488 123 | FreeSystemPageTableEntries : 12267190 124 | LongTermAverageStandbyCacheLifetimes : 14400 125 | ModifiedPageListBytes : 24715264 126 | PageFaultsPersec : 7204 127 | PageReadsPersec : 0 128 | PagesInputPersec : 0 129 | PagesOutputPersec : 0 130 | PagesPersec : 0 131 | PageWritesPersec : 0 132 | PercentCommittedBytesInUse : 55 133 | PoolNonpagedAllocs : 1182049 134 | PoolNonpagedBytes : 559136768 135 | PoolPagedAllocs : 1398128 136 | PoolPagedBytes : 831823872 137 | PoolPagedResidentBytes : 741318656 138 | StandbyCacheCoreBytes : 155684864 139 | StandbyCacheNormalPriorityBytes : 9012703232 140 | StandbyCacheReserveBytes : 7342587904 141 | SystemCacheResidentBytes : 374902784 142 | SystemCodeResidentBytes : 0 143 | SystemCodeTotalBytes : 0 144 | SystemDriverResidentBytes : 12992512 145 | SystemDriverTotalBytes : 29093888 146 | TransitionFaultsPersec : 83 147 | TransitionPagesRePurposedPersec : 0 148 | WriteCopiesPersec : 0 149 | DateTime : 11/21/2020 12:17:16 PM 150 | ComputerName : WIN10Desktop 151 | 152 | Get all memory counters for the local computer. 153 | 154 | 155 | 156 | -------------------------- Example 2 -------------------------- 157 | PS C:\> Get-MemoryPerformance -Computername $c | Select Computername,%CommittedBytes*,AvailableMBytes,CacheBytes 158 | 159 | Computername %CommittedBytesInUse AvailableMbytes CacheBytes 160 | ------------ -------------------- --------------- ---------- 161 | CHI-HVR1 7.05788756820636 14839 99377152 162 | CHI-HVR2 16.8986961377323 13144 83001344 163 | CHI-P50 24.753204334977 47143 94998528 164 | CHI-DC04 47.8621563768474 1030 180101120 165 | Win10-ENT-01 79.914213523203 1545 96137216 166 | 167 | Get selected performance data from multiple computers defined in variable c. 168 | 169 | 170 | 171 | 172 | 173 | Online Version: 174 | https://bit.ly/2TrAq9d 175 | 176 | 177 | Get-Counter 178 | 179 | 180 | 181 | Get-MemoryUsage 182 | 183 | 184 | 185 | 186 | 187 | 188 | Get-MemoryUsage 189 | Get 190 | MemoryUsage 191 | 192 | Get a snapshot of memory usage. 193 | 194 | 195 | 196 | This command will write a custom memory utilization object to the pipeline that indicates the current memory state. The criteria for what is normal or not is based on the $MemoryToolsOK and $MemoryToolsWarning global variable values. You can modify these values from the console. 197 | 198 | 199 | 200 | Get-MemoryUsage 201 | 202 | CimSession 203 | 204 | Connect using an existing CIMSession 205 | 206 | CimSession[] 207 | 208 | CimSession[] 209 | 210 | 211 | None 212 | 213 | 214 | Status 215 | 216 | Filter on a given status. 217 | 218 | 219 | All 220 | OK 221 | Warning 222 | Critical 223 | 224 | String 225 | 226 | String 227 | 228 | 229 | All 230 | 231 | 232 | 233 | Get-MemoryUsage 234 | 235 | Computername 236 | 237 | Specify the name of the computer or computers to query. 238 | 239 | String[] 240 | 241 | String[] 242 | 243 | 244 | None 245 | 246 | 247 | Status 248 | 249 | Filter on a given status. 250 | 251 | 252 | All 253 | OK 254 | Warning 255 | Critical 256 | 257 | String 258 | 259 | String 260 | 261 | 262 | All 263 | 264 | 265 | 266 | 267 | 268 | CimSession 269 | 270 | Connect using an existing CIMSession 271 | 272 | CimSession[] 273 | 274 | CimSession[] 275 | 276 | 277 | None 278 | 279 | 280 | Computername 281 | 282 | Specify the name of the computer or computers to query. 283 | 284 | String[] 285 | 286 | String[] 287 | 288 | 289 | None 290 | 291 | 292 | Status 293 | 294 | Filter on a given status. 295 | 296 | String 297 | 298 | String 299 | 300 | 301 | All 302 | 303 | 304 | 305 | 306 | 307 | System.String[] 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | Microsoft.Management.Infrastructure.CimSession[] 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | MyMemoryUsage 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 335 | 336 | 337 | 338 | 339 | -------------------------- Example 1 -------------------------- 340 | PS C:\> Get-MemoryUsage -computername Thinkp1 341 | 342 | Computername Status PctFree FreeGB TotalGB 343 | ------------ ------ ------- ------ ------- 344 | THINKP1 OK 83.66 26.39 32 345 | 346 | 347 | 348 | 349 | 350 | -------------------------- Example 2 -------------------------- 351 | PS C:\> Get-MemoryUsage dom1,srv1,srv2,win10 -Status Warning 352 | 353 | Computername Status PctFree FreeGB TotalGB 354 | ------------ ------ ------- ------ ------- 355 | WIN10 Warning 36.57 1.17 3 356 | 357 | Get usage from several computers but filter and only display those with a Warning. 358 | 359 | 360 | 361 | -------------------------- Example 3 -------------------------- 362 | PS C:\> Get-MemoryUsage dom1,srv1,srv2,win10 | Format-Table -view show 363 | 364 | Display a similar table as the previous example, but highlight using ANSI escape sequences. 365 | 366 | 367 | 368 | 369 | 370 | Online Version: 371 | https://bit.ly/2TpnLDV 372 | 373 | 374 | Show-MemoryUsage 375 | 376 | 377 | 378 | Test-MemoryUsage 379 | 380 | 381 | 382 | Get-TopProcessMemory 383 | 384 | 385 | 386 | 387 | 388 | 389 | Get-PhysicalMemory 390 | Get 391 | PhysicalMemory 392 | 393 | Get physical memory details. 394 | 395 | 396 | 397 | This command will query the Win32_PhysicalMemory class to get hardware details about installed memory. The command writes a custom object to the pipeline. 398 | 399 | 400 | 401 | Get-PhysicalMemory 402 | 403 | CimSession 404 | 405 | Connect via an existing CimSession. 406 | 407 | CimSession[] 408 | 409 | CimSession[] 410 | 411 | 412 | None 413 | 414 | 415 | 416 | Get-PhysicalMemory 417 | 418 | Computername 419 | 420 | Specify the name of a computer or computers to query. 421 | 422 | String[] 423 | 424 | String[] 425 | 426 | 427 | None 428 | 429 | 430 | 431 | 432 | 433 | CimSession 434 | 435 | Connect via an existing CimSession. 436 | 437 | CimSession[] 438 | 439 | CimSession[] 440 | 441 | 442 | None 443 | 444 | 445 | Computername 446 | 447 | Specify the name of a computer or computers to query. 448 | 449 | String[] 450 | 451 | String[] 452 | 453 | 454 | None 455 | 456 | 457 | 458 | 459 | 460 | System.String[] 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | Microsoft.Management.Infrastructure.CimSession[] 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | physicalMemoryUnit 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 488 | 489 | 490 | 491 | 492 | -------------------------- Example 1 -------------------------- 493 | PS C:\> Get-PhysicalMemory 494 | 495 | Computername: PROSPERO 496 | 497 | Manufacturer CapacityGB Form ClockSpeed Voltage Location 498 | ------------ ---------- ---- ---------- ------- -------- 499 | Crucial 32 SODIMM 2666 1200 ChannelA-DIMM0 500 | Crucial 32 SODIMM 2666 1200 ChannelB-DIMM0 501 | 502 | 503 | 504 | 505 | 506 | -------------------------- Example 2 -------------------------- 507 | PS C:\> Get-Physicalmemory -Computername thinkp1,prospero 508 | 509 | Computername: THINKP1 510 | 511 | Manufacturer CapacityGB Form ClockSpeed Voltage Location 512 | ------------ ---------- ---- ---------- ------- -------- 513 | Samsung 16 SODIMM 2667 1200 ChannelA-DIMM0 514 | Micron 16 SODIMM 2667 1200 ChannelB-DIMM0 515 | 516 | Computername: PROSPERO 517 | 518 | Manufacturer CapacityGB Form ClockSpeed Voltage Location 519 | ------------ ---------- ---- ---------- ------- -------- 520 | Crucial 32 SODIMM 2666 1200 ChannelA-DIMM0 521 | Crucial 32 SODIMM 2666 1200 ChannelB-DIMM0 522 | 523 | 524 | 525 | 526 | 527 | -------------------------- Example 3 -------------------------- 528 | PS C:\> Get-Cimsession | Get-PhysicalMemory 529 | 530 | Computername: WIN10 531 | 532 | Manufacturer CapacityGB Form ClockSpeed Voltage Location 533 | ------------ ---------- ---- ---------- ------- -------- 534 | Microsoft Corporation 2 Unknown 0 0 M0001 535 | 536 | Computername: SRV1 537 | 538 | Manufacturer CapacityGB Form ClockSpeed Voltage Location 539 | ------------ ---------- ---- ---------- ------- -------- 540 | Microsoft Corporation 2 Unknown 0 0 M0001 541 | 542 | Computername: DOM1 543 | 544 | Manufacturer CapacityGB Form ClockSpeed Voltage Location 545 | ------------ ---------- ---- ---------- ------- -------- 546 | Microsoft Corporation 3.875 Unknown 0 0 M0001 547 | Microsoft Corporation 4.125 Unknown 0 0 M0002 548 | 549 | Pipe existing CIMSessions to the command. This example querying Hyper-V virtual machines so the results are a bit different. 550 | 551 | 552 | 553 | 554 | 555 | Online Version: 556 | https://bit.ly/2HAmHKD 557 | 558 | 559 | 560 | 561 | 562 | Get-ProcessMemory 563 | Get 564 | ProcessMemory 565 | 566 | Get a snapshot of a process' memory usage. 567 | 568 | 569 | 570 | Get a snapshot of a process' memory usage based on its workingset value. You can get the same information using Get-Process or by querying the Win32_Process WMI class with Get-CimInstance. This command uses Invoke-Command to gather the information remotely. Many of the parameters are from that cmdlet. 571 | 572 | 573 | 574 | Get-ProcessMemory 575 | 576 | Name 577 | 578 | The process name. 579 | 580 | String[] 581 | 582 | String[] 583 | 584 | 585 | * 586 | 587 | 588 | Computername 589 | 590 | The name of a remote computer. 591 | 592 | String[] 593 | 594 | String[] 595 | 596 | 597 | $env:COMPUTERNAME 598 | 599 | 600 | Credential 601 | 602 | A credential for the remote computer. 603 | 604 | PSCredential 605 | 606 | PSCredential 607 | 608 | 609 | None 610 | 611 | 612 | ThrottleLimit 613 | 614 | 615 | 616 | Int32 617 | 618 | Int32 619 | 620 | 621 | 0 622 | 623 | 624 | Authentication 625 | 626 | 627 | 628 | String 629 | 630 | String 631 | 632 | 633 | Default 634 | 635 | 636 | 637 | 638 | 639 | Name 640 | 641 | The process name. 642 | 643 | String[] 644 | 645 | String[] 646 | 647 | 648 | * 649 | 650 | 651 | Computername 652 | 653 | The name of a remote computer. 654 | 655 | String[] 656 | 657 | String[] 658 | 659 | 660 | $env:COMPUTERNAME 661 | 662 | 663 | Credential 664 | 665 | A credential for the remote computer. 666 | 667 | PSCredential 668 | 669 | PSCredential 670 | 671 | 672 | None 673 | 674 | 675 | ThrottleLimit 676 | 677 | 678 | 679 | Int32 680 | 681 | Int32 682 | 683 | 684 | 0 685 | 686 | 687 | Authentication 688 | 689 | 690 | 691 | String 692 | 693 | String 694 | 695 | 696 | Default 697 | 698 | 699 | 700 | 701 | 702 | [string[]] 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | myProcessMemory 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 722 | 723 | 724 | 725 | 726 | -------------------------- EXAMPLE 1 -------------------------- 727 | PS C:\> Get-ProcessMemory code,powershell* 728 | 729 | Name Count Threads AvgMB SumMB Computername 730 | ---- ----- ------- ----- ----- ------------ 731 | Code 9 154 112.8199 1015.3789 BOVINE320 732 | powershell 3 77 179.1367 537.4102 BOVINE320 733 | powershell_ise 1 21 242.0586 242.0586 BOVINE320 734 | 735 | The default output displays the process name, the total process count, the total number of threads, the average workingset value per process in MB and the total workingset size of all processes also formatted in MB. 736 | 737 | 738 | 739 | -------------------------- EXAMPLE 2 -------------------------- 740 | PS C:\> Get-ProcessMemory -computername srv1,srv2,dom1 -Name lsass -cred company\artd 741 | 742 | Name Count Threads AvgMB SumMB Computername 743 | ---- ----- ------- ----- ----- ------------ 744 | lsass 1 30 60.1719 60.1719 DOM1 745 | lsass 1 7 10.8594 10.8594 SRV1 746 | lsass 1 7 9.6953 9.6953 SRV2 747 | 748 | 749 | 750 | 751 | 752 | -------------------------- EXAMPLE 3 -------------------------- 753 | PS C:\> Get-ProcessMemory *ActiveDirectory* -Computername dom1 | Select-Object * 754 | 755 | Name : Microsoft.ActiveDirectory.WebServices 756 | Count : 1 757 | Threads : 10 758 | Average : 46940160 759 | Sum : 46940160 760 | Computername : DOM1 761 | 762 | This example uses a wildcard for the process name because the domain controller only has one related process. The output shows the raw values. 763 | 764 | 765 | 766 | -------------------------- EXAMPLE 4 -------------------------- 767 | PS C:\> Get-ProcessMemory *edge*,firefox,chrome | sort sum -Descending 768 | 769 | Name Count Threads AvgMB SumMB Computername 770 | ---- ----- ------- ----- ----- ------------ 771 | firefox 6 189 180.4134 1082.4805 BOVINE320 772 | chrome 8 124 67.1377 537.1016 BOVINE320 773 | MicrosoftEdgeCP 4 171 66.1846 264.7383 BOVINE320 774 | MicrosoftEdge 1 37 70.1367 70.1367 BOVINE320 775 | MicrosoftEdgeSH 1 10 8.2734 8.2734 BOVINE320 776 | 777 | Get browser processes and sort on the underlying SUM property in descending order. 778 | 779 | 780 | 781 | 782 | 783 | Get-Process 784 | 785 | 786 | 787 | Get-TopProcessMemory 788 | 789 | 790 | 791 | 792 | 793 | 794 | Get-TopProcessMemory 795 | Get 796 | TopProcessMemory 797 | 798 | Display processes using the most memory based on WorkingSet. 799 | 800 | 801 | 802 | This command will use Get-CimInstance to retrieve the top processes by WorkingSet. It will retrieve the top 5 by default. The output will include a percentage of total in-use memory the process is using as well as the process owner. 803 | 804 | 805 | 806 | Get-TopProcessMemory 807 | 808 | CimSession 809 | 810 | Connect via an existing CimSession. 811 | 812 | CimSession[] 813 | 814 | CimSession[] 815 | 816 | 817 | None 818 | 819 | 820 | Top 821 | 822 | The number of processes to retrieve. There is a maximum value of 25. 823 | 824 | Int32 825 | 826 | Int32 827 | 828 | 829 | 5 830 | 831 | 832 | 833 | Get-TopProcessMemory 834 | 835 | Computername 836 | 837 | The name of a computer or computers to query. 838 | 839 | String[] 840 | 841 | String[] 842 | 843 | 844 | None 845 | 846 | 847 | Top 848 | 849 | The number of processes to retrieve. There is a maximum value of 25. 850 | 851 | Int32 852 | 853 | Int32 854 | 855 | 856 | 5 857 | 858 | 859 | 860 | 861 | 862 | CimSession 863 | 864 | Connect via an existing CimSession. 865 | 866 | CimSession[] 867 | 868 | CimSession[] 869 | 870 | 871 | None 872 | 873 | 874 | Computername 875 | 876 | The name of a computer or computers to query. 877 | 878 | String[] 879 | 880 | String[] 881 | 882 | 883 | None 884 | 885 | 886 | Top 887 | 888 | The number of processes to retrieve. There is a maximum value of 25. 889 | 890 | Int32 891 | 892 | Int32 893 | 894 | 895 | 5 896 | 897 | 898 | 899 | 900 | 901 | System.String[] 902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 | Microsoft.Management.Infrastructure.CimSession[] 910 | 911 | 912 | 913 | 914 | 915 | 916 | 917 | 918 | 919 | topProcessMemoryUnit 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 929 | 930 | 931 | 932 | 933 | -------------------------- Example 1 -------------------------- 934 | PS C:\> Get-TopProcessMemory -top 1 935 | 936 | Computername: PROSPERO 937 | 938 | ProcessID : 3060 939 | Name : powershell.exe 940 | WS(MB) : 1455.89 941 | PctUsed : 4.8277 942 | Owner : PROSPERO\Jeff 943 | Commandline : "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Version 5.1 -s -NoLogo -NoProfile 944 | 945 | Get the top process for the local computer. 946 | 947 | 948 | 949 | -------------------------- Example 2 -------------------------- 950 | PS C:\> Get-Cimsession | Get-TopProcessMemory | Sort Owner | Format-Table -GroupBy Owner -Property ProcessID,Name,WS,PctUsed 951 | 952 | Owner: COMPANY\ArtD 953 | 954 | Computername ProcessID Name WS PctUsed 955 | ------------ --------- ---- -- ------- 956 | WIN10 3520 powershell.exe 153464832 14.2925 957 | 958 | Owner: NT AUTHORITY\SYSTEM 959 | 960 | Computername ProcessID Name WS PctUsed 961 | ------------ --------- ---- -- ------- 962 | WIN10 2396 MsMpEng.exe 164093952 15.2824 963 | WIN10 1400 svchost.exe 65187840 6.0711 964 | WIN10 984 LogonUI.exe 55332864 5.1533 965 | WIN10 1056 svchost.exe 47890432 4.4601 966 | 967 | Use an existing CIMSession and get the top 5 processes. The output is sorted by the Owner property and formatted as a table. 968 | 969 | 970 | 971 | 972 | 973 | Online Version: 974 | https://bit.ly/34y1oCF 975 | 976 | 977 | Get-MemoryUsage 978 | 979 | 980 | 981 | Show-MemoryUsage 982 | 983 | 984 | 985 | 986 | 987 | 988 | Show-MemoryUsage 989 | Show 990 | MemoryUsage 991 | 992 | Display a colorized version of memory usage. 993 | 994 | 995 | 996 | This is a variation of the Get-MemoryUsage command. This version displays a colorized and formatted table using a custom view. Normal values will be displayed in green. Warnings will be displayed in yellow and Critical systems in red. The criteria for what is normal or not is based on the $MemoryToolsOK and $MemoryToolsWarning global variable values. You can modify these values from the console. 997 | 998 | 999 | 1000 | Show-MemoryUsage 1001 | 1002 | CimSession 1003 | 1004 | Connect to an existing CimSession. 1005 | 1006 | CimSession[] 1007 | 1008 | CimSession[] 1009 | 1010 | 1011 | None 1012 | 1013 | 1014 | 1015 | Show-MemoryUsage 1016 | 1017 | Computername 1018 | 1019 | The name of a computer or computers to query. 1020 | 1021 | String[] 1022 | 1023 | String[] 1024 | 1025 | 1026 | None 1027 | 1028 | 1029 | 1030 | 1031 | 1032 | CimSession 1033 | 1034 | Connect to an existing CimSession. 1035 | 1036 | CimSession[] 1037 | 1038 | CimSession[] 1039 | 1040 | 1041 | None 1042 | 1043 | 1044 | Computername 1045 | 1046 | The name of a computer or computers to query. 1047 | 1048 | String[] 1049 | 1050 | String[] 1051 | 1052 | 1053 | None 1054 | 1055 | 1056 | 1057 | 1058 | 1059 | System.String[] 1060 | 1061 | 1062 | 1063 | 1064 | 1065 | 1066 | 1067 | Microsoft.Management.Infrastructure.CimSession[] 1068 | 1069 | 1070 | 1071 | 1072 | 1073 | 1074 | 1075 | 1076 | 1077 | This command creates a formatted table 1078 | 1079 | 1080 | 1081 | 1082 | 1083 | 1084 | 1085 | 1086 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 1087 | 1088 | 1089 | 1090 | 1091 | -------------------------- Example 1 -------------------------- 1092 | PS C:\> show-memoryusage -Computername Dom1,Srv1,Srv2,Win10 1093 | 1094 | **************** 1095 | Memory Check 1096 | **************** 1097 | 1098 | Computername Status PctFree FreeGB TotalGB 1099 | ------------ ------ ------- ------ ------- 1100 | DOM1 OK 99.92 1023.2 1024 1101 | SRV1 OK 99.96 1023.63 1024 1102 | SRV2 OK 99.95 1023.51 1024 1103 | WIN10 Warning 36.89 1.19 3 1104 | 1105 | The line for Win10 would be displayed in Yellow. The other computers would be displayed in Green. 1106 | 1107 | 1108 | 1109 | 1110 | 1111 | Online Version: 1112 | https://bit.ly/2Tp7IWy 1113 | 1114 | 1115 | Get-MemoryUsage 1116 | 1117 | 1118 | 1119 | 1120 | 1121 | 1122 | Test-MemoryUsage 1123 | Test 1124 | MemoryUsage 1125 | 1126 | Test if a computer meets a specific memory threshold. 1127 | 1128 | 1129 | 1130 | This command can be used to test if memory utilization meets some criteria. There are several parameter sets for different tests. All of them can be used with -Quiet to return a simple Boolean value. 1131 | 1132 | 1133 | 1134 | Test-MemoryUsage 1135 | 1136 | CimSession 1137 | 1138 | Connect to an existing CimSession. 1139 | 1140 | CimSession[] 1141 | 1142 | CimSession[] 1143 | 1144 | 1145 | None 1146 | 1147 | 1148 | Quiet 1149 | 1150 | Suppress details and return a Boolean value. 1151 | 1152 | 1153 | SwitchParameter 1154 | 1155 | 1156 | False 1157 | 1158 | 1159 | UsedGB 1160 | 1161 | Enter the minimum amount of used memory in GB 1162 | 1163 | Double 1164 | 1165 | Double 1166 | 1167 | 1168 | None 1169 | 1170 | 1171 | 1172 | Test-MemoryUsage 1173 | 1174 | CimSession 1175 | 1176 | Connect to an existing CimSession. 1177 | 1178 | CimSession[] 1179 | 1180 | CimSession[] 1181 | 1182 | 1183 | None 1184 | 1185 | 1186 | Quiet 1187 | 1188 | Suppress details and return a Boolean value. 1189 | 1190 | 1191 | SwitchParameter 1192 | 1193 | 1194 | False 1195 | 1196 | 1197 | TotalGB 1198 | 1199 | Enter the minimum total memory in GB 1200 | 1201 | Int32 1202 | 1203 | Int32 1204 | 1205 | 1206 | None 1207 | 1208 | 1209 | 1210 | Test-MemoryUsage 1211 | 1212 | CimSession 1213 | 1214 | Connect to an existing CimSession. 1215 | 1216 | CimSession[] 1217 | 1218 | CimSession[] 1219 | 1220 | 1221 | None 1222 | 1223 | 1224 | FreeGB 1225 | 1226 | Enter the minimum free memory in GB. 1227 | 1228 | Double 1229 | 1230 | Double 1231 | 1232 | 1233 | None 1234 | 1235 | 1236 | Quiet 1237 | 1238 | Suppress details and return a Boolean value. 1239 | 1240 | 1241 | SwitchParameter 1242 | 1243 | 1244 | False 1245 | 1246 | 1247 | 1248 | Test-MemoryUsage 1249 | 1250 | CimSession 1251 | 1252 | Connect to an existing CimSession. 1253 | 1254 | CimSession[] 1255 | 1256 | CimSession[] 1257 | 1258 | 1259 | None 1260 | 1261 | 1262 | PercentFree 1263 | 1264 | Enter the minimum % free memory 1265 | 1266 | Int32 1267 | 1268 | Int32 1269 | 1270 | 1271 | 50 1272 | 1273 | 1274 | Quiet 1275 | 1276 | Suppress details and return a Boolean value. 1277 | 1278 | 1279 | SwitchParameter 1280 | 1281 | 1282 | False 1283 | 1284 | 1285 | 1286 | Test-MemoryUsage 1287 | 1288 | Computername 1289 | 1290 | Enter the name of a computer or computers to test. 1291 | 1292 | String[] 1293 | 1294 | String[] 1295 | 1296 | 1297 | None 1298 | 1299 | 1300 | PercentFree 1301 | 1302 | Enter the minimum % free memory 1303 | 1304 | Int32 1305 | 1306 | Int32 1307 | 1308 | 1309 | 50 1310 | 1311 | 1312 | Quiet 1313 | 1314 | Suppress details and return a Boolean value. 1315 | 1316 | 1317 | SwitchParameter 1318 | 1319 | 1320 | False 1321 | 1322 | 1323 | 1324 | Test-MemoryUsage 1325 | 1326 | Computername 1327 | 1328 | Enter the name of a computer or computers to test. 1329 | 1330 | String[] 1331 | 1332 | String[] 1333 | 1334 | 1335 | None 1336 | 1337 | 1338 | Quiet 1339 | 1340 | Suppress details and return a Boolean value. 1341 | 1342 | 1343 | SwitchParameter 1344 | 1345 | 1346 | False 1347 | 1348 | 1349 | UsedGB 1350 | 1351 | Enter the minimum amount of used memory in GB 1352 | 1353 | Double 1354 | 1355 | Double 1356 | 1357 | 1358 | None 1359 | 1360 | 1361 | 1362 | Test-MemoryUsage 1363 | 1364 | Computername 1365 | 1366 | Enter the name of a computer or computers to test. 1367 | 1368 | String[] 1369 | 1370 | String[] 1371 | 1372 | 1373 | None 1374 | 1375 | 1376 | Quiet 1377 | 1378 | Suppress details and return a Boolean value. 1379 | 1380 | 1381 | SwitchParameter 1382 | 1383 | 1384 | False 1385 | 1386 | 1387 | TotalGB 1388 | 1389 | Enter the minimum total memory in GB 1390 | 1391 | Int32 1392 | 1393 | Int32 1394 | 1395 | 1396 | None 1397 | 1398 | 1399 | 1400 | Test-MemoryUsage 1401 | 1402 | Computername 1403 | 1404 | Enter the name of a computer or computers to test. 1405 | 1406 | String[] 1407 | 1408 | String[] 1409 | 1410 | 1411 | None 1412 | 1413 | 1414 | FreeGB 1415 | 1416 | Enter the minimum free memory in GB. 1417 | 1418 | Double 1419 | 1420 | Double 1421 | 1422 | 1423 | None 1424 | 1425 | 1426 | Quiet 1427 | 1428 | Suppress details and return a Boolean value. 1429 | 1430 | 1431 | SwitchParameter 1432 | 1433 | 1434 | False 1435 | 1436 | 1437 | 1438 | 1439 | 1440 | CimSession 1441 | 1442 | Connect to an existing CimSession. 1443 | 1444 | CimSession[] 1445 | 1446 | CimSession[] 1447 | 1448 | 1449 | None 1450 | 1451 | 1452 | Computername 1453 | 1454 | Enter the name of a computer or computers to test. 1455 | 1456 | String[] 1457 | 1458 | String[] 1459 | 1460 | 1461 | None 1462 | 1463 | 1464 | FreeGB 1465 | 1466 | Enter the minimum free memory in GB. 1467 | 1468 | Double 1469 | 1470 | Double 1471 | 1472 | 1473 | None 1474 | 1475 | 1476 | PercentFree 1477 | 1478 | Enter the minimum % free memory 1479 | 1480 | Int32 1481 | 1482 | Int32 1483 | 1484 | 1485 | 50 1486 | 1487 | 1488 | Quiet 1489 | 1490 | Suppress details and return a Boolean value. 1491 | 1492 | SwitchParameter 1493 | 1494 | SwitchParameter 1495 | 1496 | 1497 | False 1498 | 1499 | 1500 | TotalGB 1501 | 1502 | Enter the minimum total memory in GB 1503 | 1504 | Int32 1505 | 1506 | Int32 1507 | 1508 | 1509 | None 1510 | 1511 | 1512 | UsedGB 1513 | 1514 | Enter the minimum amount of used memory in GB 1515 | 1516 | Double 1517 | 1518 | Double 1519 | 1520 | 1521 | None 1522 | 1523 | 1524 | 1525 | 1526 | 1527 | System.String[] 1528 | 1529 | 1530 | 1531 | 1532 | 1533 | 1534 | 1535 | 1536 | 1537 | System.Object 1538 | 1539 | 1540 | 1541 | 1542 | 1543 | 1544 | 1545 | 1546 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 1547 | 1548 | 1549 | 1550 | 1551 | -------------------------- Example 1 -------------------------- 1552 | PS C:\> Test-MemoryUsage 1553 | 1554 | Computername PctFree Test 1555 | ------------ ------- ---- 1556 | BOVINE320 50.53 True 1557 | 1558 | Run the default test on the local computer. The default percentage threshold is 50%. 1559 | 1560 | 1561 | 1562 | -------------------------- Example 2 -------------------------- 1563 | PS C:\> Test-MemoryUsage -FreeGB 16 1564 | 1565 | Computername FreeGB Test 1566 | ------------ ------ ---- 1567 | BOVINE320 15.93 False 1568 | 1569 | Test if the computer has at least 16GB of free memory. 1570 | 1571 | 1572 | 1573 | -------------------------- Example 3 -------------------------- 1574 | PS C:\> Test-MemoryUsage -computername CHI-P50 -TotalGB 32 -Quiet 1575 | True 1576 | 1577 | Test if the computer has at least 32GB of memory but only return True or False. 1578 | 1579 | 1580 | 1581 | -------------------------- Example 4 -------------------------- 1582 | PS C:\> Test-MemoryUsage -UsedGB 20 1583 | 1584 | Computername UsedGB Test 1585 | ------------ ------ ---- 1586 | BOVINE320 15.74 False 1587 | 1588 | Test if the computer is using at least 20GB of memory. 1589 | 1590 | 1591 | 1592 | 1593 | 1594 | Online Version: 1595 | https://bit.ly/2TFkXml 1596 | 1597 | 1598 | Get-MemoryUsage 1599 | 1600 | 1601 | 1602 | Get-MemoryPerformance 1603 | 1604 | 1605 | 1606 | Get-TopProcessMemory 1607 | 1608 | 1609 | 1610 | 1611 | -------------------------------------------------------------------------------- /formats/MyMemoryUsage.format.ps1xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | default 6 | 7 | MyMemoryUsage 8 | 9 | 10 | 11 | 12 | 13 | 16 14 | 15 | 16 | 17 | 8 18 | 19 | 20 | 21 | 8 22 | Right 23 | 24 | 25 | 26 | 8 27 | Right 28 | 29 | 30 | 31 | 8 32 | Right 33 | 34 | 35 | 36 | 37 | 38 | 39 | Computername 40 | 41 | 42 | Status 43 | 44 | 45 | PctFree 46 | 47 | 48 | FreeGB 49 | 50 | 51 | TotalGB 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | show 60 | 61 | MyMemoryUsage 62 | 63 | 64 | 65 | 66 | 67 | 16 68 | 69 | 70 | 71 | 8 72 | 73 | 74 | 75 | 8 76 | Right 77 | 78 | 79 | 80 | 8 81 | Right 82 | 83 | 84 | 85 | 8 86 | Right 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | Switch ($_.Status) { 95 | "OK" { $color = "$([char]0x1b)[38;5;47m" } 96 | "Warning" { $color = "$([char]0x1b)[38;5;227m" } 97 | "Critical" { $color = "$([char]0x1b)[38;5;198m" } 98 | } 99 | "$color$($_.Computername)$([char]0x1b)[0m" 100 | 101 | 102 | 103 | 104 | Switch ($_.Status) { 105 | "OK" { $color = "$([char]0x1b)[38;5;47m" } 106 | "Warning" { $color = "$([char]0x1b)[38;5;227m" } 107 | "Critical" { $color = "$([char]0x1b)[38;5;198m" } 108 | } 109 | "$color$($_.Status)$([char]0x1b)[0m" 110 | 111 | 112 | 113 | 114 | Switch ($_.Status) { 115 | "OK" { $color = "$([char]0x1b)[38;5;47m" } 116 | "Warning" { $color = "$([char]0x1b)[38;5;227m" } 117 | "Critical" { $color = "$([char]0x1b)[38;5;198m" } 118 | } 119 | "$color$($_.PctFree)$([char]0x1b)[0m" 120 | 121 | 122 | 123 | 124 | Switch ($_.Status) { 125 | "OK" { $color = "$([char]0x1b)[38;5;47m" } 126 | "Warning" { $color = "$([char]0x1b)[38;5;227m" } 127 | "Critical" { $color = "$([char]0x1b)[38;5;198m" } 128 | } 129 | "$color$($_.FreeGB)$([char]0x1b)[0m" 130 | 131 | 132 | 133 | 134 | Switch ($_.Status) { 135 | "OK" { $color = "$([char]0x1b)[38;5;47m" } 136 | "Warning" { $color = "$([char]0x1b)[38;5;227m" } 137 | "Critical" { $color = "$([char]0x1b)[38;5;198m" } 138 | } 139 | "$color$($_.TotalGB)$([char]0x1b)[0m" 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | -------------------------------------------------------------------------------- /formats/myProcessMemory.format.ps1xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | default 6 | 7 | myProcessMemory 8 | 9 | 10 | 11 | 12 | 13 | 14 | 20 15 | left 16 | 17 | 18 | 19 | 7 20 | right 21 | 22 | 23 | 24 | 7 25 | right 26 | 27 | 28 | 29 | 10 30 | right 31 | 32 | 33 | 34 | 10 35 | right 36 | 37 | 38 | 39 | 16 40 | right 41 | 42 | 43 | 44 | 45 | 46 | 47 | Name 48 | 49 | 50 | Count 51 | 52 | 53 | Threads 54 | 55 | 56 | [math]::Round($_.average/1MB,4) 57 | 58 | 59 | [math]::Round($_.sum/1MB,4) 60 | 61 | 62 | Computername 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /formats/physicalMemoryUnit.format.ps1xml: -------------------------------------------------------------------------------- 1 |  2 | 9 | 10 | 11 | 12 | 13 | default 14 | 15 | physicalMemoryUnit 16 | 17 | 18 | Computername 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 15 28 | left 29 | 30 | 31 | 32 | 13 33 | left 34 | 35 | 36 | 37 | 9 38 | left 39 | 40 | 41 | 42 | 13 43 | left 44 | 45 | 46 | 47 | 10 48 | left 49 | 50 | 51 | 52 | 17 53 | left 54 | 55 | 56 | 57 | 58 | 59 | 60 | Manufacturer 61 | 62 | 63 | CapacityGB 64 | 65 | 66 | Form 67 | 68 | 69 | ClockSpeed 70 | 71 | 72 | Voltage 73 | 74 | 75 | Location 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /formats/topProcessMemoryUnit.format.ps1xml: -------------------------------------------------------------------------------- 1 |  2 | 9 | 10 | 11 | 12 | 13 | default 14 | 15 | topProcessMemoryUnit 16 | 17 | 18 | Computername 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | ProcessID 28 | 29 | 30 | 31 | Name 32 | 33 | 34 | 35 | [math]::round($_.ws/1MB,2) 36 | 37 | 38 | 39 | PctUsed 40 | 41 | 42 | 43 | Owner 44 | 45 | 46 | 47 | Commandline 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /functions/Get-MemoryPerformance.ps1: -------------------------------------------------------------------------------- 1 | Function Get-MemoryPerformance { 2 | [CmdletBinding(DefaultParameterSetName = "Computername")] 3 | [alias("gmemp")] 4 | [OutputType("Selected.Microsoft.Management.Infrastructure.CimInstance")] 5 | Param( 6 | [Parameter( 7 | Position = 0, 8 | ValueFromPipeline, 9 | ValueFromPipelineByPropertyName, 10 | ParameterSetName = 'Computername' 11 | )] 12 | [ValidateNotNullOrEmpty()] 13 | [Alias("cn")] 14 | [string[]]$Computername = $env:Computername, 15 | 16 | [Parameter( 17 | ParameterSetName = 'Cim', 18 | Mandatory, 19 | ValueFromPipeline 20 | )] 21 | [ValidateNotNullOrEmpty()] 22 | [Microsoft.Management.Infrastructure.CimSession[]]$CimSession 23 | ) 24 | 25 | Begin { 26 | Write-Verbose "[BEGIN ] Starting: $($MyInvocation.MyCommand)" 27 | <# 28 | Get all memory performance counters. Assuming counters on the 29 | client are the same as on the server. Sort by name. 30 | #> 31 | $all = (Get-Counter -ListSet Memory*).counter | Sort-Object 32 | 33 | #get a list of class properties. Some of the properties don't 34 | #appear to have any values and are different than what you get 35 | #with Get-Counter 36 | $PerfClass = Get-CimClass -ClassName Win32_PerfFormattedData_PerfOS_Memory 37 | $selected = $PerfClass.CimClassProperties | Select-Object -Skip 9 -ExpandProperty Name 38 | $selected += @{Name = "DateTime"; Expression = { (Get-Date) } } 39 | $selected += @{Name = "ComputerName"; Expression = { $session.ComputerName } } 40 | Write-Verbose "[BEGIN ] PSBoundParameters" 41 | Write-Verbose ($PSBoundParameters | Out-String) 42 | } #begin 43 | 44 | Process { 45 | 46 | Write-Verbose "[PROCESS] Using parameter set $($PSCmdlet.ParameterSetName)" 47 | 48 | if ($PSCmdlet.ParameterSetName -eq 'Computername') { 49 | #create a temporary cimsession if using a computername 50 | $MyCIMSession = foreach ($item in $Computername) { 51 | Try { 52 | Write-Verbose "[PROCESS] Creating temporary CIM Session to $item" 53 | New-CimSession -ComputerName $item -ErrorAction Stop -OutVariable +tmpSess 54 | Write-Verbose "[PROCESS] Added session" 55 | } 56 | Catch { 57 | Write-Error "[$($item.toUpper())] Failed to create temporary CIM Session. $($_.exception.message)" 58 | } 59 | } #foreach item in computername 60 | } #if computername parameter set 61 | else { 62 | Write-Verbose "[PROCESS] Re-using CimSessions" 63 | $MyCimSession = $CimSession 64 | } 65 | 66 | foreach ($session in $MyCIMSession) { 67 | Try { 68 | Get-CimInstance -ClassName Win32_PerfFormattedData_PerfOS_Memory -CimSession $session | 69 | Select-Object -Property $selected 70 | } #try 71 | Catch { 72 | Write-Error "Failed to get performance data from $($session.computername.ToUpper()). $($_.exception.message)" 73 | } 74 | } #foreach 75 | 76 | #clean up 77 | if ($tmpSess) { 78 | Write-Verbose "[PROCESS] Removing temporary sessions" 79 | $tmpSess | Remove-CimSession 80 | Remove-Variable tmpsess 81 | } 82 | 83 | } #process 84 | 85 | End { 86 | Write-Verbose "[END ] Ending: $($MyInvocation.MyCommand)" 87 | } #end 88 | } 89 | -------------------------------------------------------------------------------- /functions/Get-MemoryUsage.ps1: -------------------------------------------------------------------------------- 1 | Function Get-MemoryUsage { 2 | [CmdletBinding(DefaultParameterSetName = 'Computername')] 3 | [OutputType("MyMemoryUsage")] 4 | [alias("gmem")] 5 | Param( 6 | [Parameter( 7 | Position = 0, 8 | ValueFromPipeline, 9 | ValueFromPipelineByPropertyName, 10 | ParameterSetName = 'Computername' 11 | )] 12 | [ValidateNotNullOrEmpty()] 13 | [Alias("cn")] 14 | [string[]]$Computername = $env:Computername, 15 | 16 | [ValidateSet("All", "OK", "Warning", "Critical")] 17 | [String]$Status = "All", 18 | 19 | [Parameter(ParameterSetName = 'Cim', Mandatory, ValueFromPipeline)] 20 | [ValidateNotNullOrEmpty()] 21 | [Microsoft.Management.Infrastructure.CimSession[]]$CimSession 22 | ) 23 | 24 | Begin { 25 | Write-Verbose "[BEGIN ] Starting: $($MyInvocation.MyCommand)" 26 | $MyCimSession = @() 27 | } #begin 28 | 29 | Process { 30 | Write-Verbose "[PROCESS] PSBoundParameters" 31 | Write-Verbose ($PSBoundParameters | Out-String) 32 | 33 | Write-Verbose "[PROCESS] Using parameter set $($PSCmdlet.ParameterSetName)" 34 | 35 | if ($PSCmdlet.ParameterSetName -eq 'Computername') { 36 | #create a temporary cimsession if using a computername 37 | foreach ($item in $Computername) { 38 | Try { 39 | Write-Verbose "[PROCESS] Creating temporary CIM Session to $item" 40 | $MyCIMSession += New-CimSession -ComputerName $item -ErrorAction Stop -OutVariable +tmpcs 41 | $tmpSession = $True 42 | Write-Verbose "[PROCESS] Added session" 43 | } 44 | Catch { 45 | Write-Error "[$($item.toUpper())] Failed to create temporary CIM Session. $($_.exception.message)" 46 | } 47 | } #foreach item in computername 48 | } #if computername parameter set 49 | else { 50 | Write-Verbose "[PROCESS] Re-using CimSessions" 51 | $MyCIMSession = $CimSession 52 | } 53 | 54 | foreach ($session in $MyCIMSession) { 55 | 56 | Write-Verbose "[PROCESS] Processing $($session.computername) with session ID $($session.ID)" 57 | 58 | Try { 59 | $os = Get-CimInstance -ClassName Win32_OperatingSystem -CimSession $session -ErrorAction stop 60 | } 61 | Catch { 62 | Write-Error "[$($session.Computername.toUpper())] Failed to retrieve data. $($_.exception.message)" 63 | } 64 | if ($os) { 65 | # Determine if Dynamic Memory is used 66 | $MaxDynamicMemory = (Get-Counter -Counter "\Hyper-V Dynamic Memory Integration Service\Maximum Memory, Mbytes" -ComputerName $os.PSComputerName -ErrorAction SilentlyContinue).CounterSamples.CookedValue * 1KB 67 | 68 | # Determine the amount of free memory 69 | if ($MaxDynamicMemory) { 70 | $FreeMemory = $os.FreePhysicalMemory + ($MaxDynamicMemory - $os.TotalVisibleMemorySize) 71 | $TotalMemory = $MaxDynamicMemory 72 | } 73 | else { 74 | $FreeMemory = $os.FreePhysicalMemory 75 | $TotalMemory = $os.TotalVisibleMemorySize 76 | } 77 | 78 | $pctFree = [math]::Round(($FreeMemory / $TotalMemory) * 100, 2) 79 | 80 | if ($pctFree -ge $MemoryToolsOK) { 81 | $StatusProperty = "OK" 82 | } 83 | elseif ($pctFree -ge $MemoryToolsWarning ) { 84 | $StatusProperty = "Warning" 85 | } 86 | else { 87 | #anything else is considered critical 88 | $StatusProperty = "Critical" 89 | } 90 | 91 | $obj = [PSCustomObject]@{ 92 | PSTypename = "MyMemoryUsage" 93 | Computername = $OS.PSComputername.ToUpper() 94 | Status = $StatusProperty 95 | PctFree = $pctFree 96 | FreeGB = [math]::Round($FreeMemory / 1mb, 2) 97 | TotalGB = [Int]($TotalMemory / 1mb) 98 | } 99 | 100 | #write object to the pipeline 101 | if ($Status -eq 'All') { 102 | $obj 103 | } 104 | else { 105 | #write filtered results 106 | $obj | Where-Object { $_.Status -match $Status } 107 | } 108 | #reset variables erring on the side of caution 109 | Clear-Variable OS, obj, Mycimsession 110 | 111 | } #if OS 112 | 113 | } #foreach 114 | 115 | #clean up temporary sessions 116 | if ($tmpSession) { 117 | Write-Verbose "[PROCESS] Removing temporary sessions" 118 | $tmpcs | Out-String | Write-Verbose 119 | $tmpcs | Remove-CimSession 120 | Remove-Variable -name tmpcs 121 | } 122 | } #process 123 | 124 | End { 125 | Write-Verbose "[END ] $($MyInvocation.MyCommand)" 126 | } #end 127 | 128 | } 129 | -------------------------------------------------------------------------------- /functions/Get-PhysicalMemory.ps1: -------------------------------------------------------------------------------- 1 | Function Get-PhysicalMemory { 2 | 3 | [CmdletBinding(DefaultParameterSetName = "Computername")] 4 | [alias("gpmem")] 5 | [OutputType("physicalMemoryUnit")] 6 | Param( 7 | [Parameter( 8 | Position = 0, 9 | ValueFromPipeline, 10 | ValueFromPipelineByPropertyName, 11 | ParameterSetName = 'Computername' 12 | )] 13 | [ValidateNotNullOrEmpty()] 14 | [Alias("cn")] 15 | [string[]]$Computername = $env:Computername, 16 | 17 | [Parameter( 18 | ParameterSetName = 'Cim', 19 | Mandatory, 20 | ValueFromPipeline 21 | )] 22 | [ValidateNotNullOrEmpty()] 23 | [Microsoft.Management.Infrastructure.CimSession[]]$CimSession 24 | ) 25 | 26 | Begin { 27 | Write-Verbose "[BEGIN ] Starting: $($MyInvocation.MyCommand)" 28 | Write-Verbose "[BEGIN ] PSBoundParameters" 29 | Write-Verbose ($PSBoundParameters | Out-String) 30 | 31 | #define a hash table to resolve Form factor 32 | $form = @{ 33 | 0 = 'Unknown' 34 | 1 = 'Other' 35 | 2 = 'SIP' 36 | 3 = 'DIP' 37 | 4 = 'ZIP' 38 | 5 = 'SOJ' 39 | 6 = 'Proprietary' 40 | 7 = 'SIMM' 41 | 8 = 'DIMM' 42 | 9 = 'TSOP' 43 | 10 = 'PGA' 44 | 11 = 'RIMM' 45 | 12 = 'SODIMM' 46 | 13 = 'SRIMM' 47 | 14 = 'SMD' 48 | 15 = 'SSMP' 49 | 16 = 'QFP' 50 | 17 = 'TQFP' 51 | 18 = 'SOIC' 52 | 19 = 'LCC' 53 | 20 = 'PLCC' 54 | 21 = 'BGA' 55 | 22 = 'FPBGA' 56 | 23 = 'LGA' 57 | } 58 | 59 | } #begin 60 | 61 | Process { 62 | 63 | Write-Verbose "[PROCESS] Using parameter set $($PSCmdlet.ParameterSetName)" 64 | 65 | if ($PSCmdlet.ParameterSetName -eq 'Computername') { 66 | #create a temporary cimsession if using a computername 67 | $MyCIMSession = foreach ($item in $Computername) { 68 | Try { 69 | Write-Verbose "[PROCESS] Creating temporary CIM Session to $item" 70 | New-CimSession -ComputerName $item -ErrorAction Stop -OutVariable +tmpSess 71 | Write-Verbose "[PROCESS] Added session" 72 | } 73 | Catch { 74 | Write-Error "[$($item.toUpper())] Failed to create temporary CIM Session. $($_.exception.message)" 75 | } 76 | } #foreach item in computername 77 | } #if computername parameter set 78 | else { 79 | Write-Verbose "[PROCESS] Re-using CimSessions" 80 | $MyCimSession = $CimSession 81 | } 82 | 83 | foreach ($session in $MyCIMSession) { 84 | Write-Verbose "[PROCESS] Processing $($session.computername)" 85 | Try { 86 | $data = Get-CimInstance -ClassName win32_physicalmemory -CimSession $session -ErrorAction Stop 87 | 88 | } #Try 89 | Catch { 90 | Write-Error "[$($Session.ComputerName.toUpper())] $($_.exception.message)" 91 | } 92 | if ($data) { 93 | #create a custom object 94 | foreach ($item in $data) { 95 | [PSCustomObject]@{ 96 | PSTypeName = "physicalMemoryUnit" 97 | Computername = $item.PSComputername.ToUpper() 98 | Manufacturer = $item.Manufacturer 99 | CapacityGB = $item.Capacity / 1GB 100 | Form = $form.item($item.FormFactor -as [Int]) 101 | ClockSpeed = $item.ConfiguredClockSpeed 102 | Voltage = $item.ConfiguredVoltage 103 | Location = $item.DeviceLocator 104 | } 105 | } 106 | } 107 | } #foreach 108 | #clean up 109 | if ($tmpSess) { 110 | Write-Verbose "[PROCESS] Removing temporary sessions" 111 | $tmpSess | Remove-CimSession 112 | Remove-Variable tmpsess 113 | } 114 | } #process 115 | 116 | End { 117 | Write-Verbose "[END ] Ending: $($MyInvocation.MyCommand)" 118 | } #end 119 | 120 | } 121 | -------------------------------------------------------------------------------- /functions/Get-ProcessMemory.ps1: -------------------------------------------------------------------------------- 1 | 2 | Function Get-ProcessMemory { 3 | [CmdletBinding()] 4 | [OutputType("myProcessMemory")] 5 | 6 | Param ( 7 | [Parameter(ValueFromPipeline, Position = 0)] 8 | [ValidateNotNullOrEmpty()] 9 | [string[]]$Name = "*", 10 | [ValidateNotNullOrEmpty()] 11 | [string[]]$Computername = $env:COMPUTERNAME, 12 | [PSCredential]$Credential, 13 | [Int32]$ThrottleLimit, 14 | [ValidateSet('Default', 'Basic', 'Credssp', 'Digest', 'Kerberos', 'Negotiate', 'NegotiateWithImplicitCredential')] 15 | [ValidateNotNullOrEmpty()] 16 | [String]$Authentication = "default" 17 | ) 18 | 19 | Begin { 20 | 21 | Write-Verbose "Starting $($MyInvocation.MyCommand)" 22 | 23 | $sb = { 24 | Param([string[]]$ProcessName) 25 | 26 | # a process might have multiple instances so get each one by name 27 | #group the processes to accommodate the use of wildcards 28 | $data = Get-Process -Name $ProcessName | Group-Object -Property Name 29 | $out = foreach ($item in $data) { 30 | $item.group | Measure-Object -Property WorkingSet -Sum -Average | 31 | Select-Object -Property @{Name = "Name"; Expression = { $item.name } }, 32 | Count, 33 | @{Name = "Threads"; Expression = { $item.group.threads.count } }, 34 | Average, Sum, 35 | @{Name = "Computername"; Expression = { $env:computername } } 36 | } 37 | #sort output in descending order by the sum property 38 | $out | Sort-Object -Property Sum -Descending 39 | } #close ScriptBlock 40 | 41 | #update PSBoundParameters so it can be splatted to Invoke-Command 42 | [void]$PSBoundParameters.Add("ScriptBlock", $sb) 43 | [void]$PSBoundParameters.add("HideComputername", $True) 44 | 45 | } #begin 46 | 47 | Process { 48 | 49 | [void]$PSBoundParameters.Remove("Name") 50 | Write-Verbose "Querying processes $($name -join ',') on $($Computername -join ',')" 51 | 52 | #need to make sure argument is treated as an array 53 | $PSBoundParameters.ArgumentList = , @($Name) 54 | if (-Not $PSBoundParameters.ContainsKey("Computername")) { 55 | #add the default value if nothing was specified 56 | [void]$PSBoundParameters.Add("Computername", $Computername) 57 | } 58 | $PSBoundParameters | Out-String | Write-Verbose 59 | 60 | Invoke-Command @PSBoundParameters | 61 | Select-Object -Property * -ExcludeProperty RunSpaceID, PS* | 62 | ForEach-Object { 63 | #insert a custom type name for the format directive 64 | [void]($_.PSObject.TypeNames.insert(0, "myProcessMemory")) 65 | $_ 66 | } 67 | 68 | } #process 69 | 70 | End { 71 | Write-Verbose "Ending $($MyInvocation.MyCommand)" 72 | } #end 73 | 74 | } #close function 75 | -------------------------------------------------------------------------------- /functions/Get-TopProcessMemory.ps1: -------------------------------------------------------------------------------- 1 | Function Get-TopProcessMemory { 2 | 3 | [CmdletBinding(DefaultParameterSetName = "Computername")] 4 | [alias("gtop")] 5 | [OutputType("topProcessMemoryUnit")] 6 | Param( 7 | [Parameter( 8 | Position = 0, 9 | ValueFromPipeline, 10 | ValueFromPipelineByPropertyName, 11 | ParameterSetName = 'Computername' 12 | )] 13 | [ValidateNotNullOrEmpty()] 14 | [Alias("cn")] 15 | [string[]]$Computername = $env:Computername, 16 | 17 | [Parameter( 18 | ParameterSetName = 'Cim', 19 | Mandatory, 20 | ValueFromPipeline 21 | )] 22 | [ValidateNotNullOrEmpty()] 23 | [Microsoft.Management.Infrastructure.CimSession[]]$CimSession, 24 | 25 | [validateRange(1, 25)] 26 | [Int]$Top = 5 27 | ) 28 | 29 | Begin { 30 | Write-Verbose "[BEGIN ] Starting: $($MyInvocation.MyCommand)" 31 | 32 | #a private function to get process owner 33 | function _getProcessOwner { 34 | [CmdletBinding()] 35 | Param([object]$CimProcess) 36 | $own = $cimProcess | Invoke-CimMethod -MethodName GetOwner 37 | "$($own.domain)\$($own.user)" 38 | } 39 | } #Begin 40 | 41 | Process { 42 | Write-Verbose "[PROCESS] PSBoundParameters" 43 | Write-Verbose ($PSBoundParameters | Out-String) 44 | 45 | Write-Verbose "[PROCESS] Using parameter set $($PSCmdlet.ParameterSetName)" 46 | 47 | if ($PSCmdlet.ParameterSetName -eq 'Computername') { 48 | #create a temporary cimsession if using a computername 49 | $myCIMSession = foreach ($item in $Computername) { 50 | Try { 51 | Write-Verbose "[PROCESS] Creating temporary CIM Session to $item" 52 | New-CimSession -ComputerName $item -ErrorAction Stop -OutVariable +TmpSess 53 | Write-Verbose "[PROCESS] Added session" 54 | } 55 | Catch { 56 | Write-Error "[$($item.toUpper())] Failed to create temporary CIM Session. $($_.exception.message)" 57 | } 58 | } #foreach item in computername 59 | } #if computername parameter set 60 | else { 61 | Write-Verbose "[PROCESS] Re-using CimSessions" 62 | $MyCimSession = $CimSession 63 | } 64 | 65 | foreach ($session in $MyCIMSession) { 66 | Write-Verbose "[PROCESS] SessionID = $($session.ID)" 67 | Try { 68 | Write-Verbose "[PROCESS] Querying $($session.computername.ToUpper())" 69 | $data = Get-CimInstance -ClassName win32_process -CimSession $session -ErrorAction Stop | Sort-Object WorkingSetSize -Descending | Select-Object -First $Top 70 | } #Try 71 | Catch { 72 | Write-Error "[$($Session.Computername.toUpper())] $($_.exception.message)" 73 | } #Catch 74 | if ($data) { 75 | #get overall memory usage 76 | $mu = Get-MemoryUsage -CimSession $session 77 | [int64]$used = ($mu.totalGB - $mu.FreeGB) * 1gb 78 | 79 | Foreach ($item in $data) { 80 | [PSCustomObject]@{ 81 | PSTypename = "topProcessMemoryUnit" 82 | Computername = $item.PSComputername.ToUpper() 83 | ProcessID = $item.ProcessID 84 | Name = $item.Name 85 | WS = $item.WorkingSetSize 86 | PctUsed = [math]::Round(($item.WorkingSetSize /$used) * 100, 4) 87 | CreationDate = $item.CreationDate 88 | Runtime = (Get-Date) - $item.CreationDate 89 | Owner = $(_getProcessOwner $item) 90 | Commandline = $item.Commandline 91 | } 92 | } 93 | } 94 | } #foreach 95 | #clean up 96 | if ($TmpSess.count -gt 0) { 97 | Write-Verbose "[PROCESS] Removing temporary sessions" 98 | $TmpSess | Out-String | Write-Verbose 99 | $TmpSess | Remove-CimSession 100 | Remove-Variable -name TmpSess -Force 101 | } 102 | } #process 103 | 104 | End { 105 | Write-Verbose "[END ] Ending: $($MyInvocation.MyCommand)" 106 | } #end 107 | } 108 | -------------------------------------------------------------------------------- /functions/Show-MemoryUsage.ps1: -------------------------------------------------------------------------------- 1 | Function Show-MemoryUsage { 2 | 3 | [CmdletBinding(DefaultParameterSetName = 'Computername')] 4 | [alias("shmem")] 5 | Param( 6 | [Parameter( 7 | Position = 0, 8 | ValueFromPipeline, 9 | ValueFromPipelineByPropertyName, 10 | ParameterSetName = 'Computername' 11 | )] 12 | [ValidateNotNullOrEmpty()] 13 | [Alias("cn")] 14 | [string[]]$Computername = $env:Computername, 15 | 16 | [Parameter(ParameterSetName = 'Cim', Mandatory, ValueFromPipeline)] 17 | [ValidateNotNullOrEmpty()] 18 | [Microsoft.Management.Infrastructure.CimSession[]]$CimSession 19 | ) 20 | 21 | Begin { 22 | Write-Verbose "[BEGIN ] Starting: $($MyInvocation.MyCommand)" 23 | 24 | #a formatted report title 25 | $title = @" 26 | 27 | **************** 28 | Memory Check 29 | **************** 30 | "@ 31 | 32 | "$([char]0x1b)[38;5;159m$Title$([char]0x1b)[0m" 33 | # Write-Host $title -ForegroundColor Cyan 34 | 35 | #initialize an array to hold data 36 | $data = @() 37 | } #begin 38 | 39 | Process { 40 | Write-Verbose "[PROCESS] PSBoundParameters" 41 | Write-Verbose ($PSBoundParameters | Out-String) 42 | Write-Verbose "[PROCESS] Using parameter set $($PSCmdlet.ParameterSetName)" 43 | 44 | if ($PSCmdlet.ParameterSetName -eq 'Computername') { 45 | foreach ($Computer in $Computername) { 46 | #get memory usage data for each computer 47 | $data += Get-MemoryUsage -Computername $computer 48 | } 49 | } 50 | else { 51 | foreach ($session in $CIMSession) { 52 | #get memory usage data for each computer 53 | $data += Get-MemoryUsage -CimSession $session 54 | } 55 | } 56 | } #Process 57 | 58 | End { 59 | #format the results using a custom formatting view 60 | $data | Format-Table -View show 61 | Write-Verbose "[END ] $($MyInvocation.MyCommand)" 62 | } #end 63 | 64 | } 65 | -------------------------------------------------------------------------------- /functions/Test-MemoryUsage.ps1: -------------------------------------------------------------------------------- 1 | Function Test-MemoryUsage { 2 | [CmdletBinding(DefaultParameterSetName = 'PercentComputer')] 3 | [alias("tmem")] 4 | [OutputType("Boolean", "PSCustomObject")] 5 | Param( 6 | [ValidateNotNullOrEmpty()] 7 | [Alias("cn")] 8 | [Parameter(Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = "PercentComputer")] 9 | [Parameter(Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = "FreeComputer")] 10 | [Parameter(Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = "TotalComputer")] 11 | [Parameter(Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = "UsedComputer")] 12 | [string[]]$Computername = $env:Computername, 13 | 14 | [Parameter(Mandatory, ParameterSetName = "PercentCIM")] 15 | [Parameter(Mandatory, ParameterSetName = "FreeCIM")] 16 | [Parameter(Mandatory, ParameterSetName = "TotalCIM")] 17 | [Parameter(Mandatory, ParameterSetName = "UsedCIM")] 18 | [Microsoft.Management.Infrastructure.CimSession[]]$CimSession, 19 | 20 | [Parameter(HelpMessage = "Enter the minimum % free memory", ParameterSetName = "PercentComputer")] 21 | [Parameter(HelpMessage = "Enter the minimum % free memory", ParameterSetName = "PercentCIM")] 22 | [ValidateNotNullOrEmpty()] 23 | [Int]$PercentFree = 50, 24 | 25 | [Parameter(HelpMessage = "Enter the minimum free memory in GB", Mandatory, ParameterSetName = "FreeComputer")] 26 | [Parameter(HelpMessage = "Enter the minimum free memory in GB", Mandatory, ParameterSetName = "FreeCIM")] 27 | [ValidateNotNullOrEmpty()] 28 | [double]$FreeGB, 29 | 30 | [ValidateNotNullOrEmpty()] 31 | [Parameter(HelpMessage = "Enter the minimum total memory in GB", Mandatory, ParameterSetName = "TotalComputer")] 32 | [Parameter(HelpMessage = "Enter the minimum total memory in GB", Mandatory, ParameterSetName = "TotalCIM")] 33 | [Int]$TotalGB, 34 | 35 | [Parameter(HelpMessage = "Enter the minimum amount of used memory in GB", Mandatory, ParameterSetName = "UsedComputer")] 36 | [Parameter(HelpMessage = "Enter the minimum amount of used memory in GB", Mandatory, ParameterSetName = "UsedCIM")] 37 | [ValidateNotNullOrEmpty()] 38 | [double]$UsedGB, 39 | 40 | [Switch]$Quiet 41 | ) 42 | 43 | Begin { 44 | Write-Verbose "[BEGIN ] Starting: $($MyInvocation.MyCommand)" 45 | } #begin 46 | 47 | Process { 48 | Write-Verbose "[PROCESS] PSBoundParameters" 49 | Write-Verbose ($PSBoundParameters | Out-String) 50 | 51 | if ($Computername) { 52 | $usage = foreach ($Computer in $Computername) { 53 | #get memory usage data for each computer 54 | Get-MemoryUsage -Computername $computer 55 | } 56 | } 57 | else { 58 | $usage = foreach ($session in $CIMSession) { 59 | #get memory usage data for each computer 60 | Get-MemoryUsage -CimSession $session 61 | } 62 | } 63 | 64 | Foreach ($mem in $usage) { 65 | Switch -regex ($PSCmdlet.ParameterSetName) { 66 | "Used" { 67 | Write-Verbose "[PROCESS] Testing if Used GB is >= to $UsedGB" 68 | $used = $mem.TotalGB - $mem.FreeGB 69 | Write-Verbose "[PROCESS] Used = $used" 70 | if ($Used -ge $usedGB) { 71 | $Test = $True 72 | } 73 | else { 74 | $Test = $False 75 | } 76 | $data = $mem | Select-Object Computername, @{Name = "UsedGB"; Expression = { $used } }, 77 | @{Name = "Test"; Expression = { $test } } 78 | } 79 | "Total" { 80 | Write-Verbose "[PROCESS] Testing if Total size is >= $TotalGB" 81 | if ($mem.TotalGB -ge $TotalGB) { 82 | $Test = $True 83 | } 84 | else { 85 | $Test = $False 86 | } 87 | $data = $mem | Select-Object Computername, TotalGB, @{Name = "Test"; Expression = { $test } } 88 | } 89 | "Free" { 90 | Write-Verbose "[PROCESS] Testing if Free GB is >= $FreeGB" 91 | if ($FreeGB -le $mem.FreeGB) { 92 | $Test = $True 93 | } 94 | else { 95 | $Test = $False 96 | } 97 | $data = $mem | Select-Object Computername, FreeGB, @{Name = "Test"; Expression = { $test } } 98 | } 99 | "Percent" { 100 | Write-Verbose "[PROCESS] Testing if Percent free is >= $PercentFree" 101 | if ($mem.PctFree -ge $percentFree) { 102 | $Test = $True 103 | } 104 | else { 105 | $Test = $False 106 | } 107 | $data = $mem | Select-Object Computername, PctFree, @{Name = "Test"; Expression = { $test } } 108 | } 109 | } #switch 110 | 111 | if ($Quiet) { 112 | $Test 113 | } 114 | else { 115 | $data 116 | } 117 | } #foreach $mem 118 | 119 | } #process 120 | End { 121 | Write-Verbose "[END ] Ending: $($MyInvocation.MyCommand)" 122 | } #end 123 | } 124 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016-2023 JDH Information Technology Solutions, Inc. 4 | 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | THE SOFTWARE. 25 | -------------------------------------------------------------------------------- /pre-release-changelog.md: -------------------------------------------------------------------------------- 1 | # Archived Changelog for MemoryTools 2 | 3 | Change log for the module before the first public release. 4 | 5 | ## v0.5.0 6 | 7 | - Minor code cleanup 8 | - Updated documentation (Issue #11) 9 | - Added a changelog to the repository 10 | - Updated `README.md`. 11 | 12 | ## v0.4.2 13 | 14 | - Merge branch `featureDynamicMemory` of https://github.com/IskanderNovena/MemoryTools into IskanderNovena-featureDynamicMemory (Issue #12) 15 | 16 | ## 0.4.1 17 | 18 | - Removed dev and scratch files from the module prior to publishing to PSGallery. 19 | 20 | ## v0.4.0 21 | 22 | - Added a new function Get-TopProcessMemory. (Issue #3) 23 | - Revised Verbose message formatting. 24 | - Updated Pester tests. (Issue #9) 25 | - Update module manifest. 26 | 27 | ## v0.3.1 28 | 29 | - Updated Pester tests 30 | - Fixed a typo bug with `Get-MemoryPerformance` 31 | - Fixed a bug where `computername` wasn't getting added to `Get-MemoryPerformance` 32 | - Fixed a bug where `datetime` wasn't getting added to `Get-MemoryPerformance` 33 | 34 | ## v0.3.0 35 | 36 | - Revised `Get-MemoryPerformance` to use `Get-CimInstance`. (Issue #8) 37 | - Fixed a bug in several commands that wasn't removing temporary sessions. 38 | - Published module to GitHub. 39 | 40 | ## v0.2.9 41 | 42 | - Modified `Get-Physicalmemory` to support CimSessions. 43 | - Removed debug and trace code from functions. 44 | 45 | ## v0.2.8 46 | 47 | - Modified `Test-MemoryUsage` to support CimSessions. 48 | - Fixed a bug with `Test-MemoryUsage` that calculate Used test properly. 49 | 50 | ## v0.2.7 51 | 52 | - Modified `Get-MemoryUsage` to accept CIMSessions. 53 | - working on Pester tests for CimSessions. Not Complete. 54 | 55 | ## v0.2.6 56 | 57 | - Added `MemoryToolsSettings.ps1`. 58 | - Moved alias definitions to the settings file. 59 | - Modified `Get-MemoryUsage` to use global variables from the setting file for OK and Warning levels. (Issue #5) 60 | - Revised Pester tests to reflect changes. 61 | - Updated manifest. 62 | 63 | ## v0.2.5 64 | 65 | - Modified `Get-MemoryUsage` to filter on Status. (Issue #4) 66 | - Modified Pester tests. 67 | 68 | ## v0.2.4 69 | 70 | - Fixed formatting bug with `Show-MemoryUsage`. (Issue #2) 71 | 72 | ## v0.2.3 73 | 74 | - Fixed pipelining issues. (#1) 75 | 76 | ## v0.2.0 77 | 78 | - Initial release. 79 | --------------------------------------------------------------------------------