4

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)
1
  • 1
    If your goal is just to write process object to host, you could write $proc | Out-Host. Commented Feb 3, 2021 at 21:36

3 Answers 3

5

Write-Host, whose purpose is to write directly to the display, does not use PowerShell's rich output formatting system - it uses simple .ToString() formatting instead, which often results in unhelpful representations - see this answer for details.

If you explicitly want to print to the display (host) only while using rich formatting, use
Out-Host instead
:

$proc | Out-Host # rich formatting, display output only

The Out-String cmdlet uses the same formatting and returns the formatted representation as data, in the form of a single, multi-line string (by default).

However, if there's no concern about accidentally producing data output, via PowerShell's success output stream (see about_Redirection), you can simply use PowerShell's implicit output behavior, which also results in rich formatting if the data is ultimately sent to the display (in the absence of getting captured in a variable, sent through the pipeline, or getting redirected):

# Implicit output to the success stream, which, if not captured or redirected,
# prints to the display *by default*, richly formatted.
$proc  

The above is the implicit - and generally preferable - equivalent of Write-Output $proc; explicit use of Write-Output, whose purpose is to write to the success output stream, is rarely needed; see this answer for additional information.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

Found solution that give me same output result

$proc = Get-Process -id 412
$str = Out-String -InputObject $proc
Write-Host $str

1 Comment

Simpler: ($proc = Get-Process -id 412) (note the parentheses, which cause the assigned value to be passed through). Or, if you don't want to pollute the success output stream and print to the host (console) only, ($proc = Get-Process -id 412) | Out-Host.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.