使用PowerShell,是否可以删除包含文件的目录而不提示确认操作?
当前回答
rm -Force -Recurse -Confirm:$false $directory2Delete在PowerShell ISE中不起作用,但在常规的PowerShell CLI中起作用。
我希望这能有所帮助。它快把我逼疯了。
其他回答
要删除没有文件夹的内容,您可以使用以下方法:
Remove-Item "foldertodelete\*" -Force -Recurse
2018年更新
在当前版本的PowerShell中(在Windows 10和Windows 11上使用v5.1进行测试,在2023年),可以使用更简单的Unix语法rm -R .\DirName无声地删除目录.\DirName及其可能包含的所有子目录和文件。事实上,许多常见的Unix命令在PowerShell中的工作方式与在Linux命令行中相同。
也可以使用rm -R .\DirName\* (Jeff在评论中提到了)清理文件夹,但不能清理文件夹本身。
由于我的目录在C:\users中,我必须以管理员身份运行powershell,
del ./[your Folder name] -Force -Recurse
这个命令对我很管用。
有些多级目录文件夹需要删除两次,这困扰了我很久。这是我的最终代码,它为我工作,并清理得很好,希望它有帮助。
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')
Remove-Item -LiteralPath "foldertodelete" -Force -Recurse
或者,用更短的版本
rm /path -r -force
推荐文章
- 如何复制在bash所有目录和文件递归?
- 如何在PowerShell中获得本地主机名?
- PowerShell:如何将数组对象转换为PowerShell中的字符串?
- 从PowerShell ISE中的另一个PS1脚本调用PowerShell脚本PS1
- 如何运行一个PowerShell脚本而不显示窗口?
- PowerShell:仅为单个命令设置环境变量
- 是否有一种方法可以通过双击.ps1文件来使PowerShell脚本工作?
- PowerShell等价于grep -f
- “Write-Host”,“Write-Output”,或“[console]::WriteLine”之间的区别是什么?
- c#测试用户是否有对文件夹的写权限
- Powershell相当于bash的&号(&),用于分叉/运行后台进程
- PowerShell脚本在机器上返回。net框架的版本?
- 移动一个文件到服务器上的另一个文件夹
- 如何在PowerShell中获得MD5校验和
- 如何在PowerShell格式化日期时间