anonit

Create a temporary folder in powershell

Jan 21st, 2019
163
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. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×