Cataster
Cataster

Reputation: 3481

How to redirect output from command to variable?

How do i redirect output from this command to a variable?

Restore-ASDatabase -Server $Server -RestoreFile $File -Name $CINPUT -Security:$Choice -AllowOverwrite -ErrorAction Stop

i tried this:

Restore-ASDatabase -Server $Server -RestoreFile $File -Name $CINPUT -Security:$Choice -AllowOverwrite -ErrorAction Stop >$output

$output

didnt display anything. the command iteslf does actually output something. but i want to store this output to a variable

The database restore operation completed successfully.

Upvotes: 3

Views: 8010

Answers (2)

mklement0
mklement0

Reputation: 437100

D.J.'s helpful answer shows the most straightforward way to capture any command's (success) output in a variable - by direct assignment.


As for what you tried:

>$output

>, the output redirection operator only supports redirecting to files specified by their name/path (it also supports suppressing output with >$null) - you cannot use it to save output in a variable.

Therefore, the value of $output would be interpreted as the target file name/path; if variable $output was never created, this is tantamount to > $null, i.e., output suppression.

Also note that the files that > creates are invariably plain-text files that reflect the same output formatting that you would see in the console (terminal), and as such they are not suitable for programmatic processing.


By contrast, the common -OutVariable (-ov) parameter you mention in a comment does allow you to capture a command's output objects in a variable, while not interfering with the command's output.

That is, the command's output is still (also) written to the output stream, and if that output isn't consumed (by another command, a variable assignment, or a redirection), it still prints to the console.

E.g., -OutVariable output saves a cmdlet / advanced function's output in variable $output - note the absence of $ in the -OutVariable argument:

PS> Get-Date -OutVariable output; $output

Thursday, June 27, 2019 10:17:07 PM  # direct Get-Date output
Thursday, June 27, 2019 10:17:07 PM  # output from $output

Therefore, -OutVariable (-ov) is useful:

  • if you want to see a command's output in the console while also capturing that output in a variable for later analysis.

  • if you want to capture an intermediate command's output inside a pipeline without interfering with the pipeline.


A slight caveat re -OutVariable (-ov) is that it:

  • doesn't create regular PowerShell arrays ([object[]]), but instances of [System.Collections.ArrayList].

  • creates a - single-element - [System.Collections.ArrayList] even if the command outputs only a single object (as Get-Date does, for instance).

These surprising behaviors are discussed in GitHub issue #3154.

However, given PowerShell's flexible collection handling and its member-access enumeration feature, the behaviors may not always be problematic in practice.

Upvotes: 6

D.J.
D.J.

Reputation: 4034

You save the output of a command as a variable like this:

$commandOutput = Restore-ASDatabase -Server $Server -RestoreFile $File -Name $CINPUT -Security:$Choice -AllowOverwrite -ErrorAction Stop

Upvotes: 6

Related Questions