使用PowerShell,是否可以删除包含文件的目录而不提示确认操作?
当前回答
如果你有你的文件夹作为一个对象,让我们说你在相同的脚本中使用下一个命令创建它:
$folder = New-Item -ItemType Directory -Force -Path "c:\tmp" -Name "myFolder"
然后你可以像这样在相同的脚本中删除它
$folder.Delete($true)
$true -递归删除的状态
其他回答
Remove-Item -LiteralPath "foldertodelete" -Force -Recurse
或者,用更短的版本
rm /path -r -force
从PowerShell删除强制答案: help Remove-Item说:
这个cmdlet中的递归参数不能正常工作
要解决的命令是
Get-ChildItem -Path $Destination -Recurse | Remove-Item -force -recurse
然后删除文件夹本身
Remove-Item $Destination -Force
简而言之,我们可以使用rm -r -fo {folderName}递归地删除文件夹(删除里面的所有文件和文件夹)并强制
下面是Michael Freidgeim答案的一个可复制粘贴的实现
function Delete-FolderAndContents {
# http://stackoverflow.com/a/9012108
param(
[Parameter(Mandatory=$true, Position=1)] [string] $folder_path
)
process {
$child_items = ([array] (Get-ChildItem -Path $folder_path -Recurse -Force))
if ($child_items) {
$null = $child_items | Remove-Item -Force -Recurse
}
$null = Remove-Item $folder_path -Force
}
}
Powershell使用相对文件夹。Remove-Item有几个与unix一致的有用别名。一些例子:
rm -R -Force ./directory
del -R -Force ./directory/*
推荐文章
- 如何复制在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格式化日期时间