Summary: Microsoft Scripting Guy Ed Wilson talks about using Windows PowerShell 5.0 on Windows 10 to create temporary files in the temporary folder.
Sometimes it is the little things that make life easier. You know, like a cereal bar … it’s not like a major technological breakthrough but it is much more convenient than getting a bowl of milk and opening a box of cereal and dumping it in the bowl. Or a peanut butter cup is easier to use than getting two bars of chocolate and a jar of peanut butter … neater too.
Well in Windows PowerShell 5.0 there are lots of these stealth features that make life easier and simpler for me than even Windows PowerShell 4.0. I am talking about Windows PowerShell 5.0 on Windows 10, because the downlevel package will not have all the API’s available for some of these way cool cmdlets.
Creating a temporary file in the temp folder
From time to time, I need to create a temporary file. In the old days I would create a file and give it a name that I would calculate. This wasn’t random, but it worked pretty good if I tested to ensure that the file did not already exist, and if it did exist, then I would delete it and create the file. As you might surmise, that was about three lines of code … not as bad as in the VBScript days, but still it was boring to do. I even wrote my own create a temporary file function to relieve some of the tedium.
Later, I was reading through the .NET Framework SDK and I came across a static method of the System.IO.Path class. It was called GetTempFileName. This was a bit confusing because at first everytime I called it to get a temporary file name, and then I used that temporary file name to create a temporary file, I would get an error … the call worked, but it generated an error. Later, I figured out that the GetTempFileName static method was actually misnamed, and should have been named CreateTempFile because that is what it does. I generates the temp file name AND creates the temporary file. So when I was calling the method to get the temp file name it created the file. Then when I attempted to use the temp file name to create the file … well an error occurred.
The really easy to create a temp file using PowerShell 5
The really easy to create a temp file is to use Windows PowerShell 5.0 on Windows 10 … because there is a New-TemporaryFile cmdlet. This is really simple:
PS C:\> New-TemporaryFile
Directory: C:\Users\mredw\AppData\Local\Temp
Mode LastWriteTime Length Name
—- ————- —— —-
-a—- 3/31/2016 8:05 PM 0 tmp36BB.tmp
Of course, this does not do a whole lot of good because while I have a temp file, I cannot really access it very easily because I don’t know the name and path. Well, I can figure out the path perhaps, but not the file name. So the better way to do this is to capture the output. This appears here:
PS C:\> $tmp = New-TemporaryFile
$tmp | fl *
PSPath : Microsoft.PowerShell.Core\FileSystem::C:\Users\mredw\AppData\Local\Temp\tmpBADC.tmp
PSParentPath : Microsoft.PowerShell.Core\FileSystem::C:\Users\mredw\AppData\Local\Temp
PSChildName : tmpBADC.tmp
PSDrive : C
PSProvider : Microsoft.PowerShell.Core\FileSystem
PSIsContainer : False
Mode : -a—-
VersionInfo : File: C:\Users\mredw\AppData\Local\Temp\tmpBADC.tmp
InternalName:
OriginalFilename:
FileVersion:
FileDescription:
Product:
ProductVersion:
Debug: False
Patched: False
PreRelease: False
PrivateBuild: False
SpecialBuild: False
Language:
BaseName : tmpBADC
Target : {}
LinkType :
Name : tmpBADC.tmp
Length : 0
DirectoryName : C:\Users\mredw\AppData\Local\Temp
Directory : C:\Users\mredw\AppData\Local\Temp
IsReadOnly : False
Exists : True
FullName : C:\Users\mredw\AppData\Local\Temp\tmpBADC.tmp
Extension : .tmp
CreationTime : 3/31/2016 8:07:31 PM
CreationTimeUtc : 4/1/2016 12:07:31 AM
LastAccessTime : 3/31/2016 8:07:31 PM
LastAccessTimeUtc : 4/1/2016 12:07:31 AM
LastWriteTime : 3/31/2016 8:07:31 PM
LastWriteTimeUtc : 4/1/2016 12:07:31 AM
Attributes : Archive
More than likely, I want the fullname property because it contains the path to the file as well as the file name and extension. It will be easy to use. This appears here:
PS C:\> $tmp.FullName
C:\Users\mredw\AppData\Local\Temp\tmpBADC.tmp
So, now I can use Out-File to write to the file. This appears here:
Get-Process | Out-File $tmp.FullName
notepad $tmp.FullName
Or, I can redirect to the file. This appears here:
Get-Service >> $tmp.FullName
notepad $tmp.FullName
Remove-Item $tmp.FullName -Force
When I am done, I want to use Remove-Item to delete the temporary file so that I keep things neat and tidy.
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. Also check out my Microsoft Operations Management Suite Blog. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy