Return memory usage / powershell


How do I return memory usage on a specific process?

$proc = Get-WmiObject -class win32_process -computer fvrrd06 | where-object {$_.Commandline -eq 'C:\Windows\System32\svchost.exe -k termsvcs' }

I do not see (any properties) the same memory usage as in task manager.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael PfisterCommented:
The value shown in task manager is WorkingSetPrivate which can be retrieved via WMI class Win32_PerfRawData_PerfProc_Process

value in task manager = WorkingSetPrivate / 1024

Ganesh GuruduSenior ConsultantCommented:
try this

$os = Get-Ciminstance Win32_OperatingSystem
$pctFree = [math]::Round(($os.FreePhysicalMemory/$os.TotalVisibleMemorySize)*100,2)
Michael PfisterCommented:
Can't see the command line property in this class, you may have to grab the process id from WIn32_Process and query Win32_PerfRawData_PerfProc_Process for the memory

$proc = Get-WmiObject -class win32_process -computer fvrrd06 | where-object {$_.Commandline -eq 'C:\Windows\System32\svchost.exe -k termsvcs' }
$memory =  (Get-WmiObject -class Win32_PerfRawData_PerfProc_Process -computer | where-object {$_.IDProcess -eq $proc.ProcessID}).WorkingSetPrivate 

Open in new window

Especially when querying remote machines, you shouldn't do the filtering in Powershell; filter in WMI wherever possible.
Try it like this:
$ComputerName = 'fvrrd06'
$CommandLine = 'C:\Windows\System32\svchost.exe -k termsvcs'
$ProcessId = (Get-WmiObject -ComputerName $ComputerName -Query "Select ProcessId From Win32_Process Where CommandLine='$($CommandLine.Replace('\', '\\'))'").ProcessId
$PrivateWorkSetKB = (Get-WmiObject -ComputerName $ComputerName -Query "Select WorkingSetPrivate From Win32_PerfRawData_PerfProc_Process Where IDProcess=$($ProcessId)").WorkingSetPrivate / 1KB

Open in new window

Or if you want the original properties of the Process object joined with the WorkingSetPrivate (here in bytes, like the rest):
$ComputerName = 'fvrrd06'
$CommandLine = 'C:\Windows\System32\svchost.exe -k termsvcs'
Get-WmiObject -ComputerName $ComputerName -Query "Select * From Win32_Process Where CommandLine='$($CommandLine.Replace('\', '\\'))'" |
	Select-Object -Property *, @{n='WorkingSetPrivate'; e={(Get-WmiObject -ComputerName $ComputerName -Query "Select WorkingSetPrivate From Win32_PerfRawData_PerfProc_Process Where IDProcess=$($_.ProcessId)").WorkingSetPrivate}}

Open in new window

Edit: Fixed Process ID in second block.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.