使用 PowerShell,是否可以在不提示确认操作的情况下删除某些包含文件的目录?
Remove-Item -LiteralPath "foldertodelete" -Force -Recurse
来自 PowerShell remove force answer:帮助 Remove-Item 说:
此 cmdlet 中的 Recurse 参数无法正常工作
解决方法的命令是
Get-ChildItem -Path $Destination -Recurse | Remove-Item -force -recurse
然后删除文件夹本身
Remove-Item $Destination -Force
Remove-Item -Recurse -Force $dir
,它可以工作。
Update-Help
后获得的 Windows 10 扩展 Get-Help Remove-Item
文档上问题仍然存在......
这对我有用:
Remove-Item $folderPath -Force -Recurse -ErrorAction SilentlyContinue
因此,该文件夹与其中的所有文件一起被删除,如果文件夹路径不存在,它不会产生错误。
2018 更新
在当前版本的 PowerShell(在 Windows 10 1809 上使用 v5.1 测试)中,可以使用更简单的 Unix 语法 rm -R .\DirName
以静默方式删除目录 .\DirName
及其可能包含的所有子目录和文件。事实上,许多常见的 Unix 命令在 PowerShell 中的工作方式与在 Linux 命令行中的工作方式相同。
还可以使用 rm -R .\DirName\*
清理文件夹,但不能清理文件夹本身(Jeff 在评论中指出)。
rm -R .\DirName\*
清理文件夹而不删除文件夹本身
简而言之,我们可以使用rm -r -fo {folderName}
递归删除文件夹(删除里面的所有文件和文件夹)并强制
要删除没有文件夹的内容,您可以使用以下命令:
Remove-Item "foldertodelete\*" -Force -Recurse
rm -Force -Recurse -Confirm:$false $directory2Delete
在 PowerShell ISE 中不起作用,但它通过常规 PowerShell CLI 起作用。
我希望这有帮助。它让我吃香蕉。
PowerShell CLI
调用而不是在 PowerShell ISE
中进行开发时,此文件夹已被删除。
这对我有用:
Remove-Item C:\folder_name -Force -Recurse
Powershell 适用于相对文件夹。 Remove-Item
有几个与 unix 一致的有用别名。一些例子:
rm -R -Force ./directory
del -R -Force ./directory/*
下面是 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
}
}
$LogPath = "E:\" # Your local of directories
$Folders = Get-Childitem $LogPath -dir -r | Where-Object {$_.name -like "*temp*"}
foreach ($Folder in $Folders)
{
$Item = $Folder.FullName
Write-Output $Item
Remove-Item $Item -Force -Recurse
}
由于我的目录位于 C:\users 我必须以管理员身份运行我的 powershell,
del ./[your Folder name] -Force -Recurse
这个命令对我有用。
del
是 Remove-Item
的别名。
如果您将文件夹作为对象,假设您使用下一个命令在同一脚本中创建了它:
$folder = New-Item -ItemType Directory -Force -Path "c:\tmp" -Name "myFolder"
然后你可以在同一个脚本中像这样删除它
$folder.Delete($true)
$true - 递归删除的状态
$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
}
如果要将具有固定路径的变量和作为动态路径的字符串连接成整个路径以删除文件夹,则可能需要以下命令:
$fixPath = "C:\Users\myUserName\Desktop"
Remove-Item ("$fixPath" + "\Folder\SubFolder") -Recurse
在变量 $newPath
中,连接路径现在是:"C:\Users\myUserName\Desktop\Folder\SubFolder"
因此,您可以从起点 ("C:\Users\myUserName\Desktop"
) 中删除多个目录,这些目录已在变量 $fixPath
中定义和固定。
$fixPath = "C:\Users\myUserName\Desktop"
Remote-Item ("$fixPath" + "\Folder\SubFolder") -Recurse
Remote-Item ("$fixPath" + "\Folder\SubFolder1") -Recurse
Remote-Item ("$fixPath" + "\Folder\SubFolder2") -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')
()
值的函数是不正确的......它只是说“把它当作一个数组”。
不定期副业成功案例分享
-ErrorAction Ignore
,尽管这也会隐藏其他错误。Remove-Item './folder/*'
。如果您真的只想清除所有文件夹的文件,您可以列出所有叶子并将其通过管道传送到 Remove-Item cmdletGet-ChildItem -Recurse -File | Remove-Item