Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Create Temporary Files with PowerShell 5.0

$
0
0

Summary: Ed Wilson, Microsoft Scripting Guy, talks about creating temporary files with Windows PowerShell 5.0 in Windows 10.

Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sipping a nice Gunpowder Green tea with hibiscus leaves and rose hips in it. I also added a bit of lemon grass for taste. I made the tea last night and chilled it. The taste is surprisingly clean and refreshing. Along with some Belgium crisp chocolate sticks, it makes a delightful mid-morning snack.

I am stilling playing around with Windows 10 and Windows PowerShell 5.0. I suspect this will probably be a task that will occupy me until the next versions ship. One thing I know I to have to do is clean up my profiles. The reason? Well, many of the things I had written (like the ISE Transcript function) are now included in Windows PowerShell 5.0, for example, the ISE Transcript function (see Use Transcript Tool in PowerShell ISE).

In July 2010, I wrote a function I called Out-TempFile (see How Can I Create, Display, and Then Delete a Temporary Text File?) I could use it to create temporary files. One thing that function did that the New-TemporaryFile cmdlet doesn’t do is allow me to pipe directly to a temporary file. In addition, when I was done, it would automatically delete the temporary file. So I may keep it around and update it to use New-TemporaryFile.

In Windows PowerShell 5.0, there is a new cmdlet called New-TemporaryFile. It will create a new temporary file in the temporary file location. It also returns a FileInfo object, so I can easily pick up the complete path to the file. All I need to do is to capture the return from the cmdlet. This technique is shown here:

$tmp = New-TemporaryFile

To get the complete path to the temporary file, I use the FullName property:

$tmp.FullName

If I want to output to the temporary file, I can pipe to the Out-File cmdlet, and pass the FullName parameter, like this:

Get-Process | Out-File $tmp.FullName

These commands are shown in the following image:

Image of command output

It is really important to capture the output from New-TemporaryFile cmdlet because it is basically a random file name, and therefore, it can be a major pain to find—even supposing I can remember where the temporary file storage is located. Also, this makes it easy for me to view the temp file because I can use Notepad and pass the $tmp.FullName command to it. As shown here, when I do, the output appears in Notepad:

Image of command output

When I am done, I like to clean up. Therefore, I do not want to leave the temporary file lying around. Once again, I use the FullName property from the $tmp variable:

Remove-Item $tmp.FullName -Force

That is all there is to using Windows PowerShell 5.0 to create and to delete temporary files. Join me tomorrow when I'll continue talking about way cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>