SoFunction
Updated on 2025-04-09

Windows Powershell export pipeline results

The results of the pipeline can be converted into text output, and the default is Out-Default. You can use Get-Command -verb out to view what output commands Powershell has.

Copy the codeThe code is as follows:

PS C:PowerShell> get-command -Verb out

CommandType Name         Definition
----------- ----         ----------
Cmdlet      Out-Default  Out-Default [-InputObject ]
Cmdlet      Out-File     Out-File [-FilePath]  [[-Encoding]
Cmdlet      Out-GridView Out-GridView [-InputObject ]
Cmdlet      Out-Host     Out-Host [-Paging] [-InputObject ]
Cmdlet      Out-Null     Out-Null [-InputObject ] [-Verbose]
Cmdlet      Out-Printer  Out-Printer [[-Name] ] [-InputObject
Cmdlet      Out-String   Out-String [-Stream] [-Width ]
Out-Default Sends the output to the default formatter and the default output cmdlet.
Out-File Sends the output to the file.
Out-GridView sends the output to an interactive table in a separate window.
Out-Host Sends the output to the command line.
Out-Null Removes the output and does not send it to the console.
Out-Printer Sends the output to the printer.
Out-String Sends an object to the host as a column of strings.

Absorption output results

Some commands will have output regardless of success or failure. Sometimes | Out-Null can be used when these outputs are not needed. This command functions the same as >$null. Especially in functions, it is used more often because if return is not specified specifically. The Powershell function will use the output result as the return value of the function. To avoid this trouble, usually add a command Out-Null or >$null to absorb the output results after the pipeline.

Copy the codeThe code is as follows:

PS C:PowerShell> md ABC

Table of Contents: C:PowerShell

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        2011/12/19     17:05            ABC

PS C:PowerShell> md ABD >$null
PS C:PowerShell> md ABE | Out-Null

Modify pipeline format

As discussed before, Powershell will add an Out-Default to the end of each line of command by default. Out-Default contains an Out-Host by default. Is Out-Host the hero's useless place? In fact, the layout of the pipeline can be controlled through Out-Host.
Powershell will not only automatically send the pipeline results to the output device, but will also convert the pipeline results into readable text. This automatic conversion is a bit similar to Format-Table. But relying entirely on automatic conversion sometimes encounters strange output results.
For example, when using Get-Service alone, the results will be output in the form of a table, but when using pwd; Get-Service, Service information is output in the form of a list.

Copy the codeThe code is as follows:

PS C:PowerShell> Get-Service

Status   Name               DisplayName
------   ----               -----------
Running  AdobeARMservice    Adobe Acrobat Update Service
Stopped  AeLookupSvc        Application Experience
Stopped  ALG                Application Layer Gateway Service

PS C:PowerShell> pwd;Get-Service

Path
----
C:PowerShell

Status      : Stopped
Name        : THREADORDER
DisplayName : Thread Ordering Server

Status      : Running
Name        : TrkWks
DisplayName : Distributed Link Tracking Client

The second line uses two commands, separated by semicolons. But why is Service information displayed in a list? Because it is processed by Powershell's interpreter, the second command in the above example will become:
& { pwd;Get-Service} | Out-Default
Powershell does not find specially specified layout information in the command, and will try to find clues from the first result object of the first command. And impose this layout on other commands that follow.
The best way to avoid the above problems is to specify them clearly.
pwd;Get-Service | Out-Host

Forced to display in text

Powershell's text conversion usually occurs at the end of the pipeline, but if text processing is required, it is cast to text.

Copy the codeThe code is as follows:

PS C:PowerShell> ls . -Recurse | Out-String

Table of Contents: C:PowerShell

Mode         LastWriteTime   Length Name
----         -------------   ------ ----
d---- 2011/12/19     17:05          ABC
d---- 2011/12/19     17:06          ABD
d---- 2011/12/19     17:06          ABE
d---- 2011/11/29     18:21          myscript
-a--- 2011/12/19     11:31      500

PS C:PowerShell> (ls | Out-String -Stream).gettype()
IsPublic IsSerial Name     BaseType
-------- -------- ----     --------
True     True     Object[]

Excel export object

The pipeline results are exported as text files, and the more they read, it is not convenient. Therefore, it is best to export it to Excel format "csv". Such files support the Microsoft Excel program to open and process it by default.

Copy the codeThe code is as follows:

PS C:PowerShell> Get-Service | Export-Csv
PS C:PowerShell> .

When using these export commands, do not use Format-Table in the pipeline, otherwise the export results will be abnormal and you can test them yourself. So how to choose attributes? But use Select-Object.

Html export object

Html export objects are similar to Excel exports.

Copy the codeThe code is as follows:

PS C:PowerShell> Get-Service | ConvertTo-Html -Title "ls result" | Out-File
PS C:PowerShell> .