Somewhere to put that data while your script runs

Ever writing a script and want to quickly place some data in a file while in the middle of your process? Try out [System.Io.Path]::GetTempFileName(). This method gives you a file with a random, unused file name in your local temp folder where you can write out interim information without worrying about testing whether the file exists or what to call the file etc.

Let's take a closer look...

Open up your favourite PowerShell script editor and run


You will end up with output something like this


Check in Windows Explorer - the file has actually been created so what you do next might need some consideration. If you are wanting to write data out to the file then if your output process accepts an Append style functionality then you need to do that,

$F = [System.Io.Path]::GetTempFileName() 

Get-Process | Out-File $F -Append

"Process information stored in $F" | Write-Output

otherwise you might need to actually delete this file and then use your data output process to create it.

$F = [System.Io.Path]::GetTempFileName() 
$F | Remove-Item -Force

Get-Process | Out-File $F

"Process information stored in $F" | Write-Output

I encourage you to review all the other useful methods of the [System.Io.Path] class here on MSDN.

Comments (0)

Skip to main content