Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the output from PowerShell's Format-List as a string?

Tags:

I want to write the output from a Format-List command to the event log so I need it as a string.

Is there a generic way to do this that doesn't depend on knowing what is being formatted?

like image 686
SGarratt Avatar asked Jun 16 '13 09:06

SGarratt


People also ask

How do I pipe a PowerShell output to a text file?

To send a PowerShell command's output to the Out-File cmdlet, use the pipeline. Alternatively, you can store data in a variable and use the InputObject parameter to pass data to the Out-File cmdlet. Out-File saves data to a file but it does not produce any output objects to the pipeline.

How do I get strings in PowerShell?

You can use Select-String similar to grep in UNIX or findstr.exe in Windows. Select-String is based on lines of text. By default, Select-String finds the first match in each line and, for each match, it displays the file name, line number, and all text in the line containing the match.

How do I print a string in PowerShell?

The echo command is used to print the variables or strings on the console. The echo command has an alias named “Write-Output” in Windows PowerShell Scripting language. In PowerShell, you can use “echo” and “Write-Output,” which will provide the same output.


1 Answers

Use the Out-String cmdlet:

... | Format-List | Out-String 
like image 177
Ansgar Wiechers Avatar answered Sep 19 '22 02:09

Ansgar Wiechers