3

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.

2
  • -outvariable (-ov) works. Restore-ASDatabase -Server $Server -RestoreFile $File -Name $CINPUT -Security:$Choice -AllowOverwrite -ErrorAction Stop -ov $output . (note: my PSVersion 5.1.14393.2969, other earlier versions reported -ov creates a collection stackoverflow.com/a/40666568/8397835) Commented Jun 27, 2019 at 23:46
  • You may not notice that -OutVariable always creates a [System.Collections.ArrayList] instance - even if only one object is output by the command - but it definitely still happens - and is unlikely to change - see this discussion on GitHub. Commented Jun 28, 2019 at 1:52

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

Comments

6

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.

Comments

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.