Writing a text file
We started this chapter by promising to cover writing PowerShell output to a file. Let’s do that. We have learned that we can feed the output of the Format-
cmdlets into an Out-
cmdlet and there is a specific cmdlet for writing data to a file; Out-File
.
Out-File
will write the output from any cmdlet to a text file. We can format the output first using a Format-
cmdlet if we want to, but we don’t have to; if we don’t, we get the default output of the cmdlet. Let’s try it:
Get-Process | Out-File -FilePath C:\temp\poshbook\procsRaw.txt Get-Process | Format-Table -Property Name, Id, CPU, Path, Modules | Out-File -FilePath C:\temp\poshbook\procsNoWrap.txt Get-Process | Format-Table -Property Name, Id, CPU, Path, Modules -Wrap | Out-File -FilePath C:\temp\poshbook\procsWrap.txt
As we can see, the content of the text files looks exactly the same as the screen outputs; all we’ve done is redirect the output to a file instead of...