How to Export Output to Text File in PowerShell

Migel Hewage Nimesha Feb 02, 2024
How to Export Output to Text File in PowerShell

PowerShell is one of the most important tools, consisting of a CLI (Command Line Interface). It consists of a Scripting language that can be used in automation functions and a Configuration Management framework that can be used in a CI/CD environment. PowerShell supports Windows, Linux, and macOS Operating Systems.

PowerShell commands provide their standard output as a print line in the Command Line interface. We can use several commands in PowerShell to get outputs in different outlooks.

One of the key requirements of most users is to get the output out of the Command-Line interface of PowerShell. That is to get the output to a text file. We can use certain PowerShell commands to achieve this purpose.

Export Output to a Text File in PowerShell

We will first consider an example command which usually gives a standard output. The command we are going to use here would be Compare-Object.

Compare-Object is one of the basic commands in PowerShell, which enables the users to compare two files. Then the program would output the changes found in the two documents.

Further, it points to the file where the unique line is found through Side Indicator. This command is commonly used to track changes to a file in different versions of itself.

The two input files used to compare are shown below with all their content.

Export Output to a Text File in PowerShell

Compare-Object $(Get-Content .\TestDoc1.txt) $(Get-Content .\TestDoc2.txt)

The two files used in this command have dummy data of alphabetical characters, with several lines being unique in each file. The output of the code above is like this.

InputObject SideIndicator
----------- -------------
JJ          =>
KK          =>
LL          =>
MM          =>
NN          =>
DD          <=
EE          <=
FF          <=
GG          <=

Our topic is to discuss how to get the above output to an external text file.

There are several commands to do that. Some are considered the best and the easiest.

Use Out-File Command to Export the Output of a Common Command to a Text File in PowerShell

Out-File is a command that sends the output of a PowerShell command to a file. For example, we can use the command we chose above as below.

Compare-Object $(Get-Content .\TestDoc1.txt) $(Get-Content .\TestDoc2.txt) | Out-File .\TestDoc3.txt

Then the output is not written in the PowerShell Command Line Interface but directly printed in the out file provided in the command path. The output below is what is printed in the TestDoc3.

InputObject SideIndicator
----------- -------------
JJ          =>           
KK          =>           
LL          =>           
MM          =>           
NN          =>           
DD          <=           
EE          <=           
FF          <=           
GG          <=           

Further, the image below shows the same output as the output file.

Export Output to a Text File in PowerShell - 2

Other options can be used, such as Start-Transcript and Stop-Transcript. We can use these to create a script of each command run in between.

Before starting the commands, you can input Start-Transcript, and at the end of the commands you want to be scripted, you can enter Stop-Transcript.

However, this is not the best solution to get the output of a command to a separate text file, as a scripting process records everything that is happening in the PowerShell.

Hence, we can conclude that out-file is the best solution to achieve the task in question. The output is successfully received as mentioned and shown above.

Migel Hewage Nimesha avatar Migel Hewage Nimesha avatar

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.