使用PowerShell,是否可以删除包含文件的目录而不提示确认操作?
当前回答
简而言之,我们可以使用rm -r -fo {folderName}递归地删除文件夹(删除里面的所有文件和文件夹)并强制
其他回答
这招对我很管用:
Remove-Item $folderPath -Force -Recurse -ErrorAction SilentlyContinue
因此,如果文件夹路径不存在,文件夹中的所有文件都将被删除,并且不会产生错误。
$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 -LiteralPath "foldertodelete" -Force -Recurse
或者,用更短的版本
rm /path -r -force
下面是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:如何将数组对象转换为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格式化日期时间
- PowerShell和-contains操作符
- 用Python遍历目录