| by Arround The Web | No comments

How to output to a file in PowerShell?

The output of any command is normally displayed inside the PowerShell console. But sometimes you need to store the output in an external file for later use. There are various commands in PowerShell that can be used to store output to a file, such as “Out-File”. By the use of the Out-File command, you can append and overwrite the existing output file. Moreover, “Redirect operator” and “Content” cmdlets are also utilized for the same purpose.

This blog will discuss:

How to Output to a File in PowerShell?

The above query can be resolved using the given methods/commands:

Method 1: Output to a File/Document in PowerShell Using “Out-File” Command

The core purpose of the “Out-File” cmdlet in PowerShell is to send an output to a file. This cmdlet not only sends output to a file but also creates a file to store the output inside it. However, to do so, you need to launch PowerShell as an administrator. More specifically, when the “-Append” flag is used alongside the Out-File cmdlet, it appends the data or text into an existing file.

Example 1: How to Send an Output to a File Using “Out-File” Command?

Run the below command to send an output to a file. Moreover, it will overwrite the existing data or text in the file:

> 'This will create an output file' | out-file C:\File.txt

Here, the text is added on the left side of the pipeline “|” while the “out-file” cmdlet is added on the right side alongside the file path. The pipeline will send the output of the previous command to the next command as input:

Note: PowerShell is case insensitive, so “out-file” and “Out-File” will work the same.

Now, verify whether the output to a file is appended or not by running the given command:

> Get-Content C:\File.txt

Here, the “Get-Content” command will fetch the content of the specified file:

Example 2: How to Append Text to a File Using “Out-File” Command?

Write and execute the given code to append the text or data into the existing file:

> 'This will append' | out-file C:\File.txt -Append

The added “-Append” flag assists in performing the append operation:

Run the given command for verification:

> Get-Content C:\File.txt

Method 2: Output to a File in PowerShell Using Redirect Operator

The Redirect operator is another cmdlet in PowerShell used to output data or text to a file. This function the same as those of the “Out-File” cmdlet. The single redirect operator “>” is not only used to send output to a file but also creates a new file if PowerShell is launched as an administrator. Moreover, the double redirect operator “>>” in PowerShell is used to append the output to an existing file.

Example 1: How to Send/Export an Output to a File/Document Using Redirect Operator “>”?

Utilize the single redirect operator “>” to output the text to the specified file:

>'Redirect opt' > C:\File.txt

Run the following command to verify the performed operation:

> Get-Content C:\File.txt

Example 2: How to Append Text to a File Using Redirect Operator “>>”?

For the purpose of appending text to a file, use the double redirect operator “>>” as follows:

>'Redirect append' >> C:\File.txt

Verification

> Get-Content C:\File.txt

Method 3: Output to a File in PowerShell Using “Content” Cmdlets

Another method to send output to a file is by using the “Content” cmdlets. These cmdlets are of four types, including “Set-Content”, “Add-Content”, “Get-Content”, and “Clear-Content”. However, to send output to a file we are going to use only two of them which are:

  • The “Set-Content” cmdlet is used to overwrite the data/text and additionally creates a new file if PowerShell is launched as an administrator.
  • While the “Add-Content” cmdlet is used to append the data/text file in the existing file in PowerShell.

Example 1: How to Send/Export an Output to a File Using “Set-Content” Cmdlet?

Here the “Set-Content” cmdlet is being used to send the added text to the “File.txt” as output:

> 'Overwrite' | Set-Content C:\File.txt

Verification

> Get-Content C:\File.txt

Example 2: How to Append Text to a File Using “Add-Content” Cmdlet?

Run the given command to append the text to an existing file by utilizing the “Add-Content” command:

> 'Append' | Add-Content C:\File.txt

Verification

> Get-Content C:\File.txt

The output verifies that the text was appended successfully.

Conclusion

To send/export output to a file in PowerShell, the “Out-File” cmdlet is utilized. This cmdlet not only outputs to a file but also appends the data to the existing file using the “-Append” flag. Additionally, “Redirect operator” and “Content” cmdlets can also be utilized to output data or text to a file. This tutorial has covered various aspects of output to a file in PowerShell.

Share Button

Source: linuxhint.com

Leave a Reply