2

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.

Cataster
  • 3,081
  • 5
  • 32
  • 79
  • -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 https://stackoverflow.com/a/40666568/8397835) – Cataster Jun 27 '19 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](https://github.com/PowerShell/PowerShell/issues/3154). – mklement0 Jun 28 '19 at 01:52

2 Answers2

4

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
D.J.
  • 3,644
  • 2
  • 15
  • 22
4

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775