Create a temporary directory in PowerShell?

前端 未结 8 1729
一生所求
一生所求 2021-02-01 14:36

PowerShell 5 introduces the New-TemporaryFile cmdlet, which is handy. How can I do the same thing but instead of a file create a directory? Is there a New-TemporaryDirec

8条回答
  •  滥情空心
    2021-02-01 15:08

    Expanding from Michael Kropat's answer: https://stackoverflow.com/a/34559554/8083582

    Function New-TemporaryDirectory {
      $tempDirectoryBase = [System.IO.Path]::GetTempPath();
      $newTempDirPath = [String]::Empty;
      Do {
        [string] $name = [System.Guid]::NewGuid();
        $newTempDirPath = (Join-Path $tempDirectoryBase $name);
      } While (Test-Path $newTempDirPath);
    
      New-Item -ItemType Directory -Path $newTempDirPath;
      Return $newTempDirPath;
    }
    

    This should eliminate any issues with collisions.

提交回复
热议问题