Advertisement
anonit

Create a temporary folder in powershell

Jan 21st, 2019
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $TempDirectoryValid=$false
  2. $AttemptCount=0
  3. while (!($TempDirectoryValid) -and ($AttemptCount -lt 5))
  4. {
  5.     # Set random directory to the users temp directory plus a random number
  6.     $RandomDirectoryName=get-random
  7.     $TemporaryLocation=$env:TEMP+"\"+$RandomDirectoryName
  8.     # if the temporary path already exists / exit with error
  9.     $AttemptCount++
  10.     if (!(Test-Path -path $TemporaryLocation))
  11.     {
  12.         # Create the temporary location
  13.         new-item $TemporaryLocation -type directory
  14.         $TempDirectoryValid=$true
  15.     }
  16. }
  17. if ($AttemptCount -gt 4)
  18. {
  19.     write-error "Temp folder creation attempted 5 times, failed"
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement