You can convert the results of a pipe to text output by default Out-default. You can see what output the PowerShell has by Get-command-verb out the commands.
Copy Code code 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 a 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 deletes the output and does not send it to the console.
Out-printer sends the output to the printer.
Out-string sends the object as a column string to the host.
Absorption output Results
Some commands have output regardless of success or failure, and sometimes do not need these outputs to use | Out-null, the function of this command is the same as that of > $null. Especially in functions, because if return is not specified specifically. The PowerShell function takes the output as the return value of the function. To avoid this hassle, you usually add a command out-null or > $null to absorb the output.
Copy Code code 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 pipe format
As discussed earlier, PowerShell defaults to append a out-default,out-default to the end of each line of command to include a out-host, whether out-host or idle heroes:. In fact, you can control the layout of the pipe through out-host.
PowerShell not only automatically sends the pipe results to the output device, but also converts the pipe results to readable text. This automatic conversion is a bit like format-table. But relying entirely on automatic conversion can sometimes come across very strange output.
For example, when using Get-service alone, the results are output in tabular form, but using PWD; Get-service Service information is exported as a list.
Copy Code code 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 of the PowerShell interpreter, the second command in the example above will become:
& {pwd; Get-service} | Out-default
PowerShell does not find a specially-specified layout information in the command, it tries to find clues from the first result object in the first command. And imposing this layout on the other commands immediately followed.
The best way to avoid these problems is to specify them clearly.
Pwd Get-service | Out-host
Force text to appear
PowerShell text conversions generally occur at the end of the pipeline, but are coerced to text if you need to do text processing.
Copy Code code 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 a.html
PS c:powershell> (ls | Out-string-stream). GetType ()
IsPublic isserial Name BaseType
-------- -------- ---- --------
True object[] System.Array
Excel Export Objects
Pipe results exported as text files do not look regular, the more it is not convenient to read. So it is best to export the Excel format "CSV" so that the file default supports microsft Excel programs to open and process.
Copy Code code as follows:
PS c:powershell> Get-service | Export-csv A.csv
PS c:powershell>. a.csv
When using these export commands, do not use format-table in the pipeline, otherwise the export results are not normal, you can test yourself. So how do you choose attributes? But using Select-object.
HTML Export Object
HTML export objects are similar to Excel exports.
Copy Code code as follows:
PS c:powershell> Get-service | Convertto-html-title "ls result" | Out-file a.html
PS c:powershell>. a.html