Return memory usage / powershell

Hey

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.

Why
LVL 1
mikeydkAsked:
Who is Participating?
 
oBdACommented:
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.
1
 
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

HTH
0
 
Ganesh GuruduSenior ConsultantCommented:
try this

$os = Get-Ciminstance Win32_OperatingSystem
$pctFree = [math]::Round(($os.FreePhysicalMemory/$os.TotalVisibleMemorySize)*100,2)
0
 
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.