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.
We use the following command in this case.
We can immediately open the location by the given command.
The corresponding command here is:
As expected the file is present in the right location as shown in the image.
The output of the file is shown in the below image.
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.
We will call the get-childitem and pass the output to the file using the -filepath flag.
Now we will view the contents of the file using the get-content command.
Interestingly you can see the directories.txt mentioned in the list of child items.
Now let us call the penultimate command.
Now we will view the contents of the file using the get-content command again.
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.
We can also remove the new line character from each line. Let us see what happens when we do just that.
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.
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.
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.
We can enter the output to the file based on the type of stream also.
- Warning Messages.
- Verbose output.
- Debug Message.
To be able to redirect only a particular type, we need to use only the stream number in the redirection.
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.
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.
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.
- Article Rating
- Content Relevance
- Content Clarity
- Author Rating