Create a temporary directory in PowerShell?

前端 未结 8 1790
一生所求
一生所求 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:15

    Here's my attempt:

    function New-TemporaryDirectory {
        $path = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName())
    
        #if/while path already exists, generate a new path
        while(Test-Path $path)) {
            $path = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName())
        }
    
        #create directory with generated path
        New-Item -ItemType Directory -Path $path
    }
    

提交回复
热议问题