简体   繁体   中英

PowerShell Delete folder if exists

Could you help me again with a powershell script? I want to check if multiple folders exist, if they exist then delete the complete folder. Also provide information if the folder has been deleted or information if the folder does not exist.

I now use the script below for multiple files. (thanks to good help) I want to use the same script for 1 or more folders. For example, delete folder c:\test1\ and c:test2
Folders may be deleted, even if they still contain files.

$paths =  "c:\test\1.txt", "c:\test\2.txt", "c:\test\3.txt"
foreach($filePath in $paths)
{
    if (Test-Path $filePath) {
        Remove-Item $filePath -verbose
    } else {
        Write-Host "Path doesn't exits"
    }
}

I'm not super handy with powershell, hope you can help me with this again.

Thanks Tom

  • To remove a directory (folder) that has content , you must use the -Recurse switch with Remove-Item - otherwise, an interactive confirmation prompt is presented.

  • A given path existing doesn't necessarily mean that it is a directory - it may be a file . To specifically test if a given path is a directory / file, use -PathType Container / -PathType Leaf with Test-Path .

  • While only strictly necessary when paths happen to contain [ characters, the robust way to pass literal paths is via the -LiteralPath parameter that file-processing cmdlets support - by contrast, the first positional argument typically binds to the -Path parameter (eg, Test-Path foo is the same as Test-Path -Path foo ), which interprets its argument(s) as wildcard expressions .

Applied to your use case (note that no attempt is made to distinguish files from directories):

# Input directory paths.
$paths = 'c:\test1', 'c:\test2', 'c:\test3'
foreach ($path in $paths) {
    if (Test-Path -LiteralPath $path) {
      Remove-Item -LiteralPath $path -Verbose -Recurse -WhatIf
    } else {
      "Path doesn't exist: $path"
    }
}

Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.


Another, more efficient option is to use Get-Item to get objects representing the file-system items, if they exist, and pipe them to Remove-Item :

$paths = 'c:\test1', 'c:\test2', 'c:\test3'

Get-Item -LiteralPath $paths -ErrorAction SilentlyContinue -ErrorVariable errs |
  Remove-Item -Recurse -Verbose -WhatIf

if ($errs) {
  "The following path(s) do not exist: $($errs.TargetObject)"
}

Note the use of -ErrorAction SilentlyContinue to silence errors resulting from nonexistent paths, and -ErrorVariable errs in order to collect these errors in self-chosen variable $errs .

The .TargetObject property of the [System.Management.Automation.ErrorRecord] instances collected in $errs then contains the path that triggered the error, resolved to a full path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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