programing

PowerShell에서 콘텐츠가 있는 디렉터리를 조용히 제거하는 방법

elecom 2023. 5. 8. 21:53
반응형

PowerShell에서 콘텐츠가 있는 디렉터리를 조용히 제거하는 방법

PowerShell을 사용하여 작업 확인 메시지를 표시하지 않고 파일이 포함된 일부 디렉터리를 제거할 수 있습니까?

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

또는, 짧은 버전으로

rm /path -r -force

PowerShell에서 force answer를 제거합니다. 도움말 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(2023년 Windows 10 및 Windows 11에서 v5.1로 테스트됨)에서는 더 단순한 Unix 구문을 사용할 수 있습니다.rm -R .\DirName디렉토리를 자동으로 삭제합니다..\DirName포함할 수 있는 모든 하위 디렉터리 및 파일과 함께 사용합니다.실제로 많은 일반적인 유닉스 명령어는 리눅스 명령줄에서와 동일한 방식으로 PowerShell에서 작동합니다.

또한 폴더를 정리할 수 있지만 폴더 자체는 정리할 수 없습니다.rm -R .\DirName\*(Jeff가 코멘트에 기록).

간단히 말해서, 우리는 사용할 수 있습니다.rm -r -fo {folderName}폴더를 재귀적으로 제거하고(내부의 모든 파일 및 폴더를 제거) 강제 실행

폴더가 없는 내용을 삭제하려면 다음을 사용합니다.

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

rm -Force -Recurse -Confirm:$false $directory2DeletePowerShell ISE에서는 작동하지 않았지만 일반 PowerShell CLI를 통해 작동했습니다.

이것이 도움이 되길 바랍니다.그것은 나를 배나나스로 몰아 넣었습니다.

이것은 저에게 효과가 있었습니다.

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에 있었기 때문에 관리자로서 파워셸을 실행해야 했습니다.

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

이 명령은 나에게 효과가 있었습니다.

폴더를 개체로 사용하는 경우 다음 명령을 사용하여 동일한 스크립트에서 폴더를 만들었다고 가정합니다.

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

일부 다단계 디렉터리 폴더는 두 번 삭제해야 하는데, 이것은 오랫동안 저에게 문제가 되었습니다.이것이 제 최종 코드입니다. 저에게 효과가 있고, 잘 청소됩니다. 도움이 되기를 바랍니다.

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

고정 경로와 문자열을 동적 경로로 사용하는 변수전체 경로에 연결하여 폴더를 제거하려면 다음 명령이 필요할 수 있습니다.

$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"
Remove-Item ("$fixPath" + "\Folder\SubFolder") -Recurse
Remove-Item ("$fixPath" + "\Folder\SubFolder1") -Recurse
Remove-Item ("$fixPath" + "\Folder\SubFolder2") -Recurse

언급URL : https://stackoverflow.com/questions/7909167/how-to-quietly-remove-a-directory-with-content-in-powershell

반응형