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


当前回答

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
}

2018年更新

在当前版本的PowerShell中(在Windows 10和Windows 11上使用v5.1进行测试,在2023年),可以使用更简单的Unix语法rm -R .\DirName无声地删除目录.\DirName及其可能包含的所有子目录和文件。事实上,许多常见的Unix命令在PowerShell中的工作方式与在Linux命令行中相同。

也可以使用rm -R .\DirName\* (Jeff在评论中提到了)清理文件夹,但不能清理文件夹本身。

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

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

从PowerShell删除强制答案: help Remove-Item说:

这个cmdlet中的递归参数不能正常工作

要解决的命令是

Get-ChildItem -Path $Destination -Recurse | Remove-Item -force -recurse

然后删除文件夹本身

Remove-Item $Destination -Force 

由于我的目录在C:\users中,我必须以管理员身份运行powershell,

del ./[your Folder name] -Force -Recurse

这个命令对我很管用。