ChatGPT解决这个技术问题 Extra ChatGPT

如何在 PowerShell 中悄悄删除包含内容的目录

使用 PowerShell,是否可以在不提示确认操作的情况下删除某些包含文件的目录?


O
Ohad Schneider
Remove-Item -LiteralPath "foldertodelete" -Force -Recurse

我发现在包含子目录的目录上运行时需要运行两次。第一次会出现很多“目录不为空”的错误。第二次,它没有错误地完成。
如果我只想删除文件夹的内容而不是删除文件夹?
@Kiquenet-这对我有用,如果我在路径中添加一个斜杠,所以这个例子变成了 Remove-Item .\foldertodelete* -Force -Recurse
如果您希望它忽略丢失的文件夹,您可以添加 -ErrorAction Ignore,尽管这也会隐藏其他错误。
@Kiquenet 然后您可以使用通配符删除该文件夹中的所有内容:Remove-Item './folder/*'。如果您真的只想清除所有文件夹的文件,您可以列出所有叶子并将其通过管道传送到 Remove-Item cmdlet Get-ChildItem -Recurse -File | Remove-Item
C
Community

来自 PowerShell remove force answer:帮助 Remove-Item 说:

此 cmdlet 中的 Recurse 参数无法正常工作

解决方法的命令是

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

然后删除文件夹本身

Remove-Item $Destination -Force 

如果我只想删除文件夹的内容而不是删除文件夹?
@beppe9000:我相信,是的。在最近的脚本中,我使用的是 Remove-Item -Recurse -Force $dir,它可以工作。
好的,但我刚刚读到,在运行 Update-Help 后获得的 Windows 10 扩展 Get-Help Remove-Item 文档上问题仍然存在......
Get-ChildItem 还应该具有 -Force 参数,以便它还返回隐藏的文件/文件夹。
我已将此答案模块化为一个函数 - 请参阅 here
n
necrifede

这对我有用:

Remove-Item $folderPath -Force  -Recurse -ErrorAction SilentlyContinue

因此,该文件夹与其中的所有文件一起被删除,如果文件夹路径不存在,它不会产生错误。


这不会删除文件夹。
@variable,当我尝试时它删除了该文件夹。
d
divenex

2018 更新

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

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


您也可以使用 rm -R .\DirName\* 清理文件夹而不删除文件夹本身
在 2016 服务器核心计算机上,powershell 命令和这个命令都不适合我。他们都说,‘不能被删除,因为它不是空的’。我还尝试了 Windows 中的 rd 命令。我可以将文件夹移动到任何地方,只是无法删除。
S
Salman

简而言之,我们可以使用rm -r -fo {folderName}递归删除文件夹(删除里面的所有文件和文件夹)并强制


感谢您使用 -r 和 -fo。这样命令就非常简洁了。
我仍然想知道为什么 powershell 不只是从 linux 继承整个命令,比如 rm -rf folder/
E
Eric Pohl

要删除没有文件夹的内容,您可以使用以下命令:

Remove-Item "foldertodelete\*" -Force -Recurse

P
Peter Mortensen

rm -Force -Recurse -Confirm:$false $directory2DeletePowerShell ISE 中不起作用,但它通过常规 PowerShell CLI 起作用。

我希望这有帮助。它让我吃香蕉。


谢谢,我也一样!最后,当从 PowerShell CLI 调用而不是在 PowerShell ISE 中进行开发时,此文件夹已被删除。
v
variable

这对我有用:

Remove-Item C:\folder_name -Force -Recurse

I
Imtiaz Shakil Siddique

Powershell 适用于相对文件夹。 Remove-Item 有几个与 unix 一致的有用别名。一些例子:

rm -R -Force ./directory
del -R -Force ./directory/*

u
user2426679

下面是 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
    }
}

C
CDspace
$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
}

在上面的脚本中,我打印文件夹的全名并将其删除。 \o/
O
OMKAR AGRAWAL

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

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

这个命令对我有用。


顺便说一句:delRemove-Item 的别名。
D
Dmitriy Reznikov

如果您将文件夹作为对象,假设您使用下一个命令在同一脚本中创建了它:

$folder = New-Item -ItemType Directory -Force -Path "c:\tmp" -Name "myFolder"

然后你可以在同一个脚本中像这样删除它

$folder.Delete($true)

$true - 递归删除的状态


C
Community
$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
}

在上面的脚本中,我打印文件夹的全名并将其删除。一份好工作...
嗨安德森,如果您不打算添加 2 个不同的答案,您应该编辑答案,也许您想删除其中一个?
S
SwissCodeMen

如果要将具有固定路径的变量和作为动态路径的字符串连接成整个路径以删除文件夹,则可能需要以下命令:

$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

A
Aspirant Zhang

一些多级目录文件夹需要删除两次,困扰了我很久。这是我的最终代码,它对我有用,并且清理得很好,希望对您有所帮助。

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')

这只是已经发布的答案的包装。你在你的答案中看到了比其他人有什么好处? ///// 同样,在 PoSh 中调用带有 () 值的函数是不正确的......它只是说“把它当作一个数组”。