Create a temporary directory in PowerShell?

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

    Here's a variant of user4317867's answer. I create a new directory in the user's Windows "Temp" folder and make the temp folder path available as a variable ($tempFolderPath):

    $tempFolderPath = Join-Path $Env:Temp $(New-Guid)
    New-Item -Type Directory -Path $tempFolderPath | Out-Null
    

    Here's the same script available as a one-liner:

    $tempFolderPath = Join-Path $Env:Temp $(New-Guid); New-Item -Type Directory -Path $tempFolderPath | Out-Null
    

    And here's what the fully qualified temp folder path ($tempFolderPath) looks like:

    C:\Users\MassDotNet\AppData\Local\Temp\2ae2dbc4-c709-475b-b762-72108b8ecb9f
    

提交回复
热议问题