Quick PowerShell Tip: Generating random filenames

by ravikanthchaganti on June 30, 2010
78 Views

While answering a question on Windows PowerShell forum, I came across a few interesting methods in the System.IO.Path namespace. One such method is GetRandomFileName(). This method can be used to generate a random, and cryptographically strong string that you can use as a folder or a file name. The usage is quite simple.

#This uses .NET framework namespace
[System.IO.Path]::GetRandomFileName()

Also, there is another method GetTempFileName(). This method also generates a random (kinda.!) file name but always with a .TMP extension. Not just that, it also a creates a zero byte file on the disk and returns full path to the file.

#This uses .NET framework namespace
[System.IO.Path]::GetTempFileName()

There are many uses to these two methods. More on that later.!

Related Posts

{ 1 trackback }

Leave a Comment

Previous post:

Next post: