PowerShell Remove-Item not waiting

后端 未结 4 1768
不知归路
不知归路 2021-01-13 05:20

If have this piece of code

if(Test-Path -Path $OUT) 
{ 
    Remove-Item $OUT -Recurse 
}
New-Item -ItemType directory -Path $OUT

Sometimes

4条回答
  •  青春惊慌失措
    2021-01-13 05:54

    For completeness' sake: You can also use the safe and fast .NET methods:

    if ([System.IO.Directory]::Exists($OUT)) {
        [System.IO.Directory]::Delete($OUT, $true)
    }
    [System.IO.Directory]::CreateDirectory($OUT)
    

    Note:

    Depending on where you get the value of $OUT you might want to convert it to a full path first to make sure the .NET methods remove the correct directory (see @mklement0`s comment):

    $fullPath = Convert-Path $OUT
    

提交回复
热议问题