in

WindowsWindows Loving ItLoving It

[2020] PowerShell redirect output file out-file & > & >>

Redirect the output to a file using the out-file operator and > and >> operator. Use the -append, -nonewline and more flags in PowerShell.

PowerShell out file

Even from the days of DOS, we could redirect the output to a file.  Out-File is PowerShell way of doing it.

We can quickly demonstrate the out-file command by writing the list of services to a file.  We can achieve that by the below command.

get-service | out-file -filepath <YourFilePath>

We use the following command in this case.

get-service | out-file -filepath E:\services.txt
redirect to a file
redirect to a file

We can immediately open the location by the given command.

explorer <YouFileLocation>

The corresponding command here is:

explorer e:\
open file
open file

As expected the file is present in the right location as shown in the image.

output file
output file

The output of the file is shown in the below image.

content of the file
content of the file

We have to keep in mind that this command will delete all the existing content of the file and make new entries.  To prevent that and keep the existing command we have to use the -append flag.  We have demonstrated that below.

We will write the directory list of the current directory into the file and save it to the same location.  We will execute the same command and we will see that the contents are not duplicated.  But if we execute the same command with the -append command, the contents will be duplicated because the content from the previous command will not be deleted.

To begin with, we will save the file path in a variable as shown in the image.

$d  = “e:\directories.txt”

We will call the get-childitem and pass the output to the file using the -filepath flag.

get-childitem | out-file -filepath $d

Now we will view the contents of the file using the get-content command.

get-content $d
output to a file
output to a file

Interestingly you can see the directories.txt mentioned in the list of child items.

directories.txt
directories.txt

Now let us call the penultimate command.

get-childitem | out-file -filepath $d

Now we will view the contents of the file using the get-content command again.

get-content $d
text not appending
text not appending

We can see that the content of the file has not changed.  Now we will add the -append flag and see that the content gets appended.

append the content of the file
append the content of the file

We can also remove the new line character from each line.  Let us see what happens when we do just that.

get-childitem | out-file -filepath $d -nonewline
remove new line
remove new line

We can also limit the number of characters in each line with the -width flag.

The width of each line is limited to 25 characters.

get-childitem | out-file -filepath $d -width 25
limit line width
limit line width

We can write the file using a much older technique also.  It is by using the > operator.  We will use it in the below command.

get-childitem > $d
output to a file
output to a file

This technique is very easy and much easier than the out-file.  Let us look into different customization of this command.

We can append the new content after the old content using the following modification.

get-childitem >> $d

We can enter the output to the file based on the type of stream also.

  1. Success.
  2. Errors.
  3. Warning Messages.
  4. Verbose output.
  5. Debug Message.

To be able to redirect only a particular type, we need to use only the stream number in the redirection.

for example,

get-childitem 1> $d

Here the output of the get-childitem is redirected to the file.

If we want to redirect the output to a file as well as use the output on the console, we can use the tee operator.

get-childitem | tee $d
output to console and file
output to console and file

Note that we have to use the pipe operator and tee in conjunction to get the output in a file as well as the console.

Conclusion:

In this post, we learned various methods to redirect the output of a PowerShell script to a file.  We can use the out-file command as well as the > operator for this purpose.  We get many options like, append, limit the width and more with the out-file command.

If you want to know more about PowerShell commands or any of the above commands in detail, please let us know in the comments below.  For more awesome posts, stay tuned to TeKangaroo.

powershell outfile
  • Article Rating
  • Content Relevance
  • Content Clarity
  • Author Rating
4.6

Leave a Reply

Your email address will not be published. Required fields are marked *

Loading…

0

Comments

0 comments