In this video, learn how to capture the output of a pipeline by using one of three different cmdlets to create or add to a clear text file.
- [Narrator] When a cmdlet, function or script generates output, there are different destinations you may choose for that output. You may want to simply display it on the screen. You may want to push it down the pipeline for further processing, or you might want to save it for later. If you want to use it later within the same script, you may choose to push it into a variable. But if you want to use it outside of your script, the best option is for you to save that information to a file.
The out file cmdlet can be used at the end of a pipeline to redirect screen output to a text file. And the syntax of the out file cmdlet is not that tricky. At the end of a pipeline of cmdlets, let's make something simple here like Get-Date. I can simply pipe that output through the cmdlet Out-File. I'm going to need to specify the file path.
So let me go ahead and put that in the documents folder for the current profile. I can use an environment variable of #Env:USERPROFILE% That will select the profile of the currently logged in user. Then I can just say let's put this in the Documents folder. And let's create a file named Output.txt This is going to create a new file named Output.txt in my Documents directory.
If a file with this name already exists, that file will be replaced with this new one. I could add a parameter at the end of -NoClobber to avoid overriding an existing file. Or if I'm trying to create a running log where each entry gets added to an existing file, I could change this parameter and use -Append instead. The effect of this is every time I run this pipeline, this new date will be added to the end of this text file, creating a running log of sorts.
There are two separate cmdlets that replace Out-File when you're trying to add content to or set the content in an existing text file. The two cmdlets that we're going to take a look at are Add-Content and Set-Content. They can be used at the end of a pipeline just like Out-File, or they can be used as a single cmdlet to add a string to a text file.
Take a look at this first example, and how we've used Add-Content as the cmdlet. And we've specified not only the path of the file where we want to add information, and we've simply added the -Value parameter, and in quotes we've specified the string that we want to add to that text file. Let me go ahead and maximize this window so that we can see more on the screen at a time. Take a look at the second example where the first thing we do is generate the data and then we pipe that through the Add-Content cmdlet.
Again, we specify a path with the full path and filename but we've already generated the data, so we don't need the -Value option. At the end of this pipeline, I've added a parameter for Add-Content of -NoNewLine. That's because, by default, there will be a new blank line added each time you supplement your existing file with more data. The Get-Date cmdlet already adds a new line, so this is deleting the extra space in-between.
I do want to circle back on a comment I made earlier. I talked about powershell not being case sensitive. Here's an example where the specific object that we're working with is case sensitive and it matters. The capital Ms in the format of the data are for month, and the lowercase Ms are for minutes. That's important, and if you mix those up, you might get some odd results in your output. There's another cmdlet that I want to show before we close out this video.
Here in these two lines we've added content to a file, but we never verified to see if that file was there. In fact, we never checked to see if this folder was there. We used an environment variable and a common library name that we feel comfortable we can count on. But take a look at this other example. The first thing that we do is we create a new child item or a new item. The type is folder, and we're naming this folder ScriptOutput.
Presently, if I look in the root of C:, we'll find that I have no ScriptOutput directory. So right now it's safe for me to create that folder. But what if the folder already existed? Would we still want to run that cmdlet? The cmdlet that I want to show is Test-Path. Test-Path, followed by the parameter -Path and a location will return either a true or a false, either a 1 or a 0 as a pass/fail test if this folder exists.
So Test-Path with the parameter -Path C:\ScriptOutput We're going to test the path. If it does exist it's going to return true. If it does not exist it's going to return false. We only want to proceed if it's false. So I did add -Not to the beginning here. So the result is if there is not a path by this name at this location, then continue on a run this function or this script, or this block of code.
And it's going to start by creating a new item, a directory, and then inside that directory it's going to set the content, meaning we're starting from scratch in this text file named Created.txt in the folder that we just made. And in this text file we're going to add the text. "This directory was created" and then we're going to get the date with month, day and year, hour and minute, and pipe that through Add-Content to the same file.
So now when I click the green arrow here to go ahead and run my script, it will do that in the space below and I can go back to the Windows Explorer look on the C: drive, there is ScriptOutput, there's the file, Created.txt And the contents of the file are "This directory was created" with a date and time. So our script was successful. Including the creation of the folder and the creation of the file with its contents based on the output of the Test-Path cmdlet.
You can learn more about logical tests and other loops and features in courses on automated scripts and advanced functions within powershell scripts. But for now, let's turn our attention back to the use of text files. As we continue, we're going to take a look at how to read the information from a text file so that we can do something with it.
- Creating a PowerShell script
- Creating scripts in IDE or Visual Studio Code
- Working with files
- Loading modules
- Using functions
- Managing servers with WMI
- Adding users to a domain