0

Windows 10 64-bit. PowerShell 5.1

Remove directories / sub-directories when path could be any of 50 using PowerShell 5 or >.

Remove livebolivar.com and it's sub-directories from any of 50 folders named mmddyy

"%USERPROFILE%\desktop\websites\"any of 50 folders named mmddyy"\livebolivar.com"

Remove %USERPROFILE%\desktop\websites\011920\livebolivar.com

Finds too much:

gci %USERPROFILE%\desktop\websites -recurse | Where-Object {($_.PSIsContainer)} | Foreach { if ( $_.Name -eq "livebolivar.com") {remove-item $_.fullname -confirm}} 

Did not work:

gci -exclude favorites %USERPROFILE%\desktop\websites -recurse | Where-Object {($_.PSIsContainer)} | Foreach { if ( $_.Name -eq "livebolivar.com") {remove-item $_.fullname -confirm}} 

This deleted the subfolders:

$path= @("%USERPROFILE%\desktop\websites\*\livebolivar.com")
$folders= gci -path $path -Recurse | Where-Object {$_.PsIsContainer} |Group-Object {$_.FullName.Split('_')[0] }
ForEach($folder in $folders){
$folder.Group | % { Remove-Item $_.fullname -recurse -force}} 

Make test folders:

pushd %USERPROFILE%\Desktop
foreach($i in -10..-1){
$z=(Get-Date).AddDays($i).tostring("MMddyy")
ni -itemtype directory $z\livebolivar.com\New Folder > $null}
ni -itemtype file $z\livebolivar.com\New Folder\test.txt > $null}
popd 
exit 

Recurse directories remove directories recursively remove directories and sub-directories with remove-item remove directories and sub-directories with ri

9
  • It's hard to make sense of your question. What is the folder structure? Where would you use a wildcard? Does this return the folders you want to delete: gci "$Home\Desktop\Websites" -dir -recurse | ? Name -like 'livebolivar.com'? Commented Mar 17, 2020 at 0:56
  • Is checking for a six-digit parent folder close enough? gci "$Home\Desktop\Websites" -dir -recurse | ? { ($_.Name -like 'livebolivar.com') -and ($_.Parent.Name -match '\d{6}')} Commented Mar 17, 2020 at 1:19
  • So they all have six-digit parent folders. If they don't you need to edit you question providiing explicit examples of what is being included that shouldn't be. Commented Mar 17, 2020 at 1:22
  • What paths were returned by the last query that weren't valid??? Commented Mar 17, 2020 at 1:32
  • I did not understand your comments. I plugged your gci into my remove-item command and your gci works.
    – somebadhat
    Commented Mar 17, 2020 at 1:53

1 Answer 1

0

Windows 10 64-bit. PowerShell 5.1

Recursively remove directory with wildcard.

gci $env:USERPROFILE\desktop\websites\*\livebolivar.com | % {ri $_.fullname -recurse -force -whatif}

% Performs an operation against each item in a collection of input objects.

$_ represents the current item from the pipe.

.fullname is path\name.ext

No sub-directories? -force is not needed

gci $env:USERPROFILE\desktop\websites\*\livebolivar.com | % {ri $_.fullname -recurse -whatif} 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .