9

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

3
  • 2
    Did you try replacing the $paths with paths to 1 or more folders? What happens if you do? Commented Feb 17, 2022 at 15:26
  • Hi Mathias, i already tried that. Unfortunately it doesn't work. It still expects a file. Commented Feb 17, 2022 at 16:15
  • 1
    But what happens? Does it throw an error? If yes, please post the error message, in full. Commented Feb 17, 2022 at 16:17

1 Answer 1

14
  • 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 (e.g., 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ofc the "bold uppercase" were meant ironically. ;) Anyway I guess adding some emphasis to the note about WhatIf is a good thing. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.