Home
Manage Your Code
Snippet: Recursively Delete Item in PowerShell (PowerShell)
Title: Recursively Delete Item in PowerShell Language: PowerShell
Description: This function recursively deletes an item from the current location. The item can be a file or folder, and can be specified with or without wildcards. Views: 134
Author: Dave Donaldson Date Added: 12/1/2009
Copy Code  
function CleanupItem([string]$item)
{
    if (($item.StartsWith("*") -eq $true) -or ($item.EndsWith("*") -eq $true))
    {
        get-childitem -recurse -force | where {$_.name -like $item} | foreach ($_) {remove-item $_.fullname -recurse -force}
    }
    else
    {
        get-childitem -recurse -force | where {$_.name -eq $item} | foreach ($_) {remove-item $_.fullname -recurse -force}
    }
}
Usage
CleanupItem "svn"
CleanupItem "*svn"
CleanupItem "svn*"
CleanupItem "*svn*"