powershelloutput

How get output to a variable


I have a simple powershell script

$proc = Get-Process -id 412
$proc

it's return such output

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName                                                                                                                                                                
-------  ------    -----      -----     ------     --  -- -----------                                                                                                                                                                
    434      26    65768     109144       6,42    412   1 browser

how can i get this output to a variable so i can use it somewhere else in script?

I tryed to use it likes this

$proc = Get-Process -id 412
Write-Host $proc

but it gives me not same output as $proc is a instance of "System.Diagnostics.Process" class

System.Diagnostics.Process (browser)

Solution

  • It depends on exactly what you mean. $proc is an object with properties. If you do $x = $proc | out-string then $x will be the string representation of the default view. However in terms of using it later you might like to do write-host $proc.Handles $proc.NPM $proc.PM $proc.WS $proc.CPU $proc.id $proc.SI $proc.ProcessName to access each of the individual elements.