From the course: Complete Guide to PowerShell 7

Using Out-File for file output - PowerShell Tutorial

From the course: Complete Guide to PowerShell 7

Using Out-File for file output

- [Instructor] The Out-File cmdlet is a useful tool that directs the output of scripts to a text file, allowing users to save console output for future reference or documentation. The cmdlet provides flexibility by enabling you to choose between a pending new data to existing files or overwriting it entirely ensuring you have control over how output is stored. Additionally, out-file allows you to specify different encoding formats, which is essential for maintaining compatibility with various systems or applications that may require specific text and coding. By utilizing out-file, you can effectively capture and preserve results for later review, making it any valuable resource for anyone who needs to document script outputs or analyze results over time. Understanding the syntax and parameters of out-file is critical for effective usage. By specifying options like FilePath and Append, you can control where and how output is saved, ensuring compatibility and preventing data loss. Now, for file encoding options, UTF8 or the Universal Standard is considered the universal standard for text file encoding. It is capable of encoding all possible characters in the Unicode character set, making it highly versatile. This encoding is particularly advantageous in scenarios where documents need to be shared across different platforms and systems as it ensures that special characters are rendered correctly. The ACII or American Standard Code for Information Interchange is a lightweight encoding designed for symbol text files. It consists of 128 characters including standard English letters, digits, and some control characters. While it is efficient and straightforward, its limitation lies in its inability to represent characters outside the basic English alphabet. Unicode is encoding standard that provides a unique number for every character, regardless of the platform, program, or language. It supports a vast array of characters from different languages and scripts, enabling effective communication in a global context. Unicode is integral in modern computing as it facilitates the display and manipulation of text in various languages paving the way for better interoperability. File output is an essential feature in scripting and programming that offers a multitude of benefits, particularly when it comes to data persistence. By saving results to a file, users can retain information it outputs for future reference. This is especially valuable in environments where historical data is crucial for analysis, troubleshooting, or compliance purposes. Another compelling benefit of the file output is its role in automation. By utilizing tools like out-file to log results, scripts can automatically generate and maintain records of their execution. This not only streamlines workflows, but also reduces the likelihood of human error associated with manual login. Lastly, file output greatly simplifies collaboration among team members. By saving output in a portable format, teams can easily share important data without the need for complex configurations or specialized tools. This accessibility fosters a collaborative environment where insights can be swiftly exchanged and integrated into varying workflows. So let's look at common use cases, for example, logging. So we can use a parameter, such as Get-Date on one of the cmdlets and we can say out-file and put that into a specific text file and append the values to it. We could also save a specific report, for example, by saying Get-Service, which will list me all of the services and then just out those values to a specific report. So we can either have a one-time snapshot, or we can actually go and append the values on a constant basis. Now, of course, when working with any kind of data within PowerShell, you will need to kind of output information into various formats. So the first example here we're going to talk about is using what's called Out-File. The Out-File cmdlet is really just around outputting information to a flat file. So first off, we can see we've got the get dash process. So if I just execute that, that's going to give me all the processes that are executing on this machine and then going to use a pipe to pipe that array of values into an out-file and then effectively just output that to a flat file. So if I run that selection, sure enough, it goes ahead and does it. If I say, let's just do Processes txt, you can see it gives me a text file with all those values in. So really, really straightforward, just simple logic of outputting something as a file. Now, we could also have another property available to us within that which is an append option. So what this one does is we already have the processes TXT, but I actually want to get the date of today, and I want to output that into that same file and just append it. So let's just look at our last command. So if we launch this, and if I just scroll all the way to the bottom here, you can see it's just a list and it stops at WUDF Host. So nothing spectacular. So let's just close that. But if I do Get-Date Out-File and then Append, and then if we execute that specific file again, and if we scroll all the way down, you can see it now says Monday, February 3rd. So we've appended to an existing file that's already there. So that's what's nice about the out-file is that we're able to say, go and generate me an out-file and then just keep adding to it. So especially from a, if you think of logging, for example, logging things, errors, updates, transcripts of what took place, you can use this. We can also format it as well in a way that we want to see it presented in the other file. So I'm going to use Get-Service. So let's right click here. It's going to list me all of the Windows services. I'm going to format that into a different-sized table. So I'm going to have it a different way around, which will show property name. It'll show name and status. So you can see we've got the name of the service and the status, and then I want to output that file. So let's just do Get-Service, run that one there. You can ignore the red error. And then I'm going to say I want to go ahead and execute Services.txt. And when that loads, you can see it renders them all out in that format, running started, stopped, et cetera. So it puts them in that table format, not necessarily in what could be quite a complex CSV file. We could also redirect output, and you'll see we're utilizing this kind of weird shape parameter here. So like two and the chevron. What that one allows me to do is, for example, I'm going to do a search for a non-existing service. So that's going to give me an error, and I'm going to say, I would like to put that out into an error log. Now, what does that mean it took place? So if I say, error text, it'll say, "cannot Get-Service, cannot find it," et cetera. So we can use that two and chevron to output to a specific file. It's the same as if I was using out-file and writing the full thing that's there. Obviously, we can also redirect both the output streams as well because you get an output stream, and you get an error stream. So every time an action is performed, you get both. So this is done by using the star in that one. So I can say, get me a combined one, so let's execute this one here, run the selection, and then I'm going to go and open up the combined text and what do I get this time? Now, it just gives me that same error because there was nothing else that was retrieved. But if I was utilizing a service that says, well, hey, go and get me the output and everything else, then it would output both of those values together. Now, of course, what we can do is actually go ahead and get both of those. We can get the actual stream of the data and the error. Now, of course, what's going to happen here is because I'm only looking for one specific value, but let's just adjust this and then say WinRM, for example. So now, I'm going to say get me the non-existent service and the WinRM and then output that to the combined file. So let me just say Get-Service here, run that selection. And then, of course, I'm going to say, get me the combined, and you'll see it now gives me the error and gives me the output as well. So I can get both of those. Now, that's not particularly helpful because that means that my file is kind of messed up a little bit. So let me just modify this one here. So let's say I want to get WinRM as well, but this time around, you can see I'm using one chevron and 2 chevron. So errors get outputted to number two and the output goes into number one. So let me just go here and say run selection. And then first off, we'll do the OutputLog first, and you'll see there's my list. And then if I run the second one and say ErrorLog, you'll see it gives me my error. So we can start to separate the streams that take place within PowerShell. So that's one thing that's nice is that as we are trying to go through and find specific information or check that something works or want to have output that handles errors and one that handles what we're looking for, then we can separate those out very simply.

Contents