使用PowerShell,是否可以删除包含文件的目录而不提示确认操作?


当前回答

有些多级目录文件夹需要删除两次,这困扰了我很久。这是我的最终代码,它为我工作,并清理得很好,希望它有帮助。

function ForceDelete {
    [CmdletBinding()]
    param(
        [string] $path
    )
    
    rm -r -fo $path
    
    if (Test-Path -Path $path){
        Start-Sleep -Seconds 1
        Write-Host "Force delete retrying..." -ForegroundColor white -BackgroundColor red
        rm -r -fo $path
    }
}

ForceDelete('.\your-folder-name')
ForceDelete('.\your-file-name.php')

其他回答

rm -Force -Recurse -Confirm:$false $directory2Delete在PowerShell ISE中不起作用,但在常规的PowerShell CLI中起作用。

我希望这能有所帮助。它快把我逼疯了。

$LogPath = "E:\" # Your local of directories
$Folders = Get-Childitem $LogPath -dir -r | Where-Object {$_.name -like "*grav*"} # Your keyword name directories

foreach ($Folder in $Folders) 
{
    $Item =  $Folder.FullName
    Write-Output $Item
    Remove-Item $Item -Force -Recurse -ErrorAction SilentlyContinue
}

Powershell使用相对文件夹。Remove-Item有几个与unix一致的有用别名。一些例子:

rm -R -Force ./directory
del -R -Force ./directory/*

这招对我很管用:

Remove-Item C:\folder_name -Force -Recurse
Remove-Item -LiteralPath "foldertodelete" -Force -Recurse

或者,用更短的版本

rm /path -r -force