If you ever have the need to create temporary file-names relating to your temporary folder, the methods GetRandomFileName and GetTempPath from the class System.IO.Path can be used. I say relating to because the code doesn’t actually create the file. That will have to be another step if that’s what you require.
The PowerShell way to achieve this is as follows.
$tempPath = [System.IO.Path]::GetTempPath() $tempFileName = [System.IO.Path]::GetRandomFileName() $tempResult = Join-Path $tempPath $tempFileName
The method GetTempPath, returns the path of the current user’s temporary folder. This gives me:
The method GetRandomFileName returns a cryptographically strong, random string that can be used as either a folder name or a file name. This will return (for example):
bearing in mind that a different file name will be returned each time you execute the method.
To make use of all this, we can join the above information together with the Join-Path CmdLet. In the examples used, the variable tempResult, gets assigned the value: