#PSTip Generate a zero-byte, temporary file on disk

Note: This tip requires PowerShell 1.0 or above.

In an earlier tip, we showed you how to create a file of the specified size. In today’s post, let us see how we can use another .NET class – System.IO.Path – to create a temporary, zero-byte file on the disk.

PS> [System.IO.Path]::GetTempFileName()
C:\Users\ravi\AppData\Local\Temp\tmp2D48.tmp

The GetTempFileName() method can be quite useful when you need to generate a random temporary file for writing log information from a script or for saving state of your script while the execution is in progress. As shown above, this method returns full path of the newly created temporary file and always creates temporary files with an extension .TMP.

Share on: