Delete folder if it exists in PowerShell
I assume you're just trying to avoid the error message in case it doesn't exist.

What if you just ignore it:



Remove-Item $folder -Recurse -ErrorAction Ignore


If that's not what you want, I would recommend writing your own function:

function Remove-ItemSafely {
[CmdletBinding(SupportsShouldProcess=$true)]
param(
    [Parameter(
        Mandatory=$true,
        ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true
    )]
    [String[]]
    $Path ,

    [Switch]
    $Recurse
)

    Process {
        foreach($p in $Path) {
            if(Test-Path $p) {
                Remove-Item $p -Recurse:$Recurse -WhatIf:$WhatIfPreference
            }
        }
    }
}