How To Write To A Text File With PowerShell
Think you can do better? Show us!
In this video, Brandon will show us use to both the Add-Content cmdlet and .NET to write to large files in PowerShell.
Powershell has a number of ways to write text to files. I'm going to show you some of the more common methods and highlight when you might use one over the other let's get started. Probably the simplest way to write out text is with the out file commandlet if you've watched my snip about stream redirection. You can think about file as a replacement for the redirection operators and it works, much the same just pipe it. Some text and give it a file path to write to. But there's a pretty big gotcha with this command that which I'll highlight here in this example. We get all the files in the current folder and write out the text just as you'd see in the console. Notice we have a really long file here in the output text has been cut off just as it would have in the console. Now we can work around this with the not so great method of specifying a large width for the output. But this is pretty error. Prone we can't guarantee that will know the correct width to provide ahead of time, so I don't recommend this method. Now let's look at another method was set content. We can also pipe text to this commandlet to write to a new file or overwrite an existing file. We also have the option of setting the file encoding if we need to do that. This commandlet doesn't have the ability to append to the file, though, will need to use the add content commandlet for that. It's the same process with added content just pipe is some text and provide a file path to append to. This is useful if you're in a loop and need to append log data or status information to the file as you go along. You can also choose not to add a newline after each execution if you need that option. Now let's talk about speed a side effect of powershell doing so much for us is that sometimes it doesn't do it in the most efficient way, sometimes we need to do a little more work ourselves. You can see in this example. We're using out file to write out to the paragraph that text file 10000 times. If we wrapped this statement in measure command to see how long the whole operation took you'll see this simple loop took almost 20 seconds to complete out file is definitely not the most efficient way to write text to disk. Now will test the add content commandlet. And you can see the same operation with add content is not any better. In fact, this operation took over 52 seconds to complete. I think we can do better than this. The nice thing about powershell is that when you need performance .net is right under the covers so we can utilize dot net classes and methods when we need the best performance possible. Here will use the system dot IO dot stream writer class to perform the same thing is add content will create a new instance of the stream writer and Loop 10000 times and run the write line method to add each Line of the paragraph variable. But often when using .net classes and methods. There's a little more work. We need to do ourselves and in this case. We need to make sure we closed a file when we're finished powerShell will keep an open file handle if we don't do this and we can run into issues later. And you can see what the stream writer class. This operation only took 187 milliseconds. I think it's pretty clear that this is the go to method if you need to write text out to disk often and not have your script to slow to a crawl. And that covers some simple ways to write text data to disk using powershell.