# Memory.ps1, Version 2.01 # Display total and free amounts of physical memory in Bytes, KB and MB # # Usage: ./Memory.ps1 [ remote_computer ] # # Written by Rob van der Woude # http://www.robvanderwoude.com param( [string]$computer = "." ) if ( $HOME[0] -eq "/" ) { # Linux # Ignores computer name # Uses /proc/meminfo # Tip by silver_moon on Linux.com: # https://www.linux.com/blog/5-commands-check-memory-usage-linux [string[]]$freeStr = ( ( Select-String -Path /proc/meminfo -Pattern "MemAvailable" ) -Split ":" )[3].Trim( ) -Split "\s+" [string[]]$physStr = ( ( Select-String -Path /proc/meminfo -Pattern "MemTotal" ) -Split ":" )[3].Trim( ) -Split "\s+" [int64]$free = [int64]$freeStr[0] * ( "1" + $freeStr[1] ) [int64]$phys = [int64]$PhysStr[0] * ( "1" + $physStr[1] ) } else { # Windows # Uses WMI # Code generated by WMIGen: # http://www.robvanderwoude.com/wmigen.php $free = ( Get-WMIObject Win32_OperatingSystem -ComputerName $computer ).FreePhysicalMemory * 1KB $phys = ( Get-WMIObject Win32_OperatingSystem -ComputerName $computer ).TotalVisibleMemorySize * 1KB } if ( $computer -ne "." ) { Write-Host Write-Host "Computer: $computer" } Write-Host Write-Host "Physical memory".PadRight( 16, " " ) -NoNewline Write-Host "Bytes".PadLeft( 14, " " ) -NoNewline Write-Host "KB".PadLeft( 11, " " ) -NoNewline Write-Host "MB".PadLeft( 8, " " ) Write-Host ( "=" * 49 ) Write-Host "Available".PadRight( 16, " " ) -NoNewline Write-Host ([string]$free).PadLeft( 14, " " ) -NoNewline Write-Host ([string][int]($free / 1KB)).PadLeft( 11, " " ) -NoNewline Write-Host ([string][int]($free / 1MB)).PadLeft( 8, " " ) Write-Host "Total".PadRight( 16, " " ) -NoNewline Write-Host ([string]$phys).PadLeft( 14, " " ) -NoNewline Write-Host ([string][int]($phys / 1KB)).PadLeft( 11, " " ) -NoNewline Write-Host ([string][int]($phys / 1MB)).PadLeft( 8, " " ) Write-Host