繁体   English   中英

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

[英]How to quietly remove a directory with content in PowerShell

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

Remove-Item -LiteralPath "foldertodelete" -Force -Recurse

PowerShell remove force 回答:help Remove-Item 说:

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

解决方法的命令是

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

然后删除文件夹本身

Remove-Item $Destination -Force 

这对我有用:

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

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

2018 更新

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

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

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

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

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

简而言之,我们可以使用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
    }
}

这对我有用:

Remove-Item C:\folder_name -Force -Recurse
$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

这个命令对我有用。

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

rm -R -Force ./directory
del -R -Force ./directory/*
$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
}

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

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

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

$folder.Delete($true)

$true - 递归删除的状态

如果你想连接一个变量固定的路径和一个字符串作为动态路径成为一个整体路径删除的文件夹,您可能需要下面的命令:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM