How to output to a file in PowerShell?

In command-line applications, the output of any command can be saved to an external file. Each command-line application provides a command to copy the output to a file. In PowerShell, you have to use the “Out-File” cmdlet to copy the command’s output to the file. The “Out-File” cmdlet is the command of PowerShell core libraries. It is used to put the command’s output to an external file.

This post describes the possible way to get the output to a file in PowerShell.

How to output to a file in PowerShell

The Out-File command is used to output the text/output to a file. The Out-File cmdlet uses the formatting system of PowerShell to write output into the file. The following set of examples explains the usage of Out-File to get the output to a file in PowerShell.

Example 1: How to save the content to a text file in PowerShell?
The text written in the PowerShell console can also be output to a text file using the Out-File cmdlet. The following command will save the string into the “C:\Test\Output.text” file.

> "Print to output file" | Out-File -FilePath C:\Test\Output.txt

Let’s verify the content of the “C:\Test\Output.txt” file as follows:

> Get-content C:\Test\Output.txt

The output shows that the string is successfully stored in the “C:\Test\Output.txt” file. `

Example 2: How to get the processes in an external file using PowerShell?
Get-Process command is used to get the list of the processes on your computer system. In PowerShell, if you want to print out all the processes, you must use the Get-Process cmdlet. In the following command, the Out-File cmdlet is used with the Get-Process cmdlet to save the output into the “C:\Test\process.txt” file:

> Get-process | Out-File C:\Test\process.txt

The output of the Get-Process cmdlet will be stored in the “C:\Test\process.txt” file. Verify the content stored in the process.txt file by issuing the following command:

> Get-content C:\Test\process.txt

The output shows that the list of processes is stored in the “process.txt” file.

Example 3: How to append the output into the existing content in PowerShell?
By default, the Out-File command will overwrite everything in the text file. The PowerShell’s “Append” option can be used to address this issue. The following command will append (instead of replacing) the output to the existing content of the “C:\Test\Output.txt”.

> "We appended the text" | Out-File -Append C:\Test\Output.txt

As you can see from the above process, the output written in the PowerShell console was appended to the “Output.txt” file. Let’s get the content of the “C:\Test\Output.txt” file via the following command:

> Get-Content C:\Test\Output.txt

From the output, it is observed that the second line is appended to the existing content of the file.

Example 4: How to output to a file in a CSV format using PowerShell?
We can also create an output to a CSV file format. In PowerShell, writing a CSV file will take much more time as you need to create a structure of the CSV file inside the terminal’s window.

The following command will export the processes to the external file “C:\Test\CSV.txt”:

> Get-Process | Export-Csv C:\Test\CSV.txt

The command will store the data into a text file which will be stored at the specified path. The following command is used to verify the above command’s functionality:

> Get-Content C:\Test\CSV.txt

As shown in the above output, the columns are replaced with commas.

Conclusion

In PowerShell, the “Out-FIle” cmdlet is used to get the command’s output in a file. The Out-File cmdlet saves the output to a file the same as displayed on the PowerShell’s console. This post demonstrates the working of the Out-File cmdlet to copy the content to an output file. For a better understanding, we have demonstrated various examples to output to a file in PowerShell.



from https://ift.tt/sShCV4X

Post a Comment

0 Comments