- No PowerShell Get-ChildItem, o
[]denota um intervalo, para evitar isso, não use-Path, mas-LiteralPathdentro doWhere-Object - Para evitar possíveis irritações com
$env:pathescolha um diferente nome da variável.
$Mypath = "C:\path\to\directory"
Get-ChildItem -Path $Mypath -Recurse -Force |
Where-Object { $_.PSIsContainer -and (Get-ChildItem -LiteralPath $_.FullName -Recurse -Force |
Where-Object { !$_.PSIsContainer }) -eq $null } |
Remove-Item -Force -Recurse -Confirm
Em versões mais recentes do PowerShell, você pode usar Get-ChildItem parameters -Directoy e -File em vez de Where {$_.PSIsContainer}
$Mypath = "C:\path\to\directory"
Get-ChildItem -Path $Mypath -Recurse -Force -Directory -EA 0 |
Where-Object { (Get-ChildItem -LiteralPath $_.FullName -Recurse -Force -File -EA 0 ) -eq $null } |
Remove-Item -Force -Recurse -Confirm
-
-EA 0é uma abreviação de-ErrorAction SilentlyContinue - durante o teste, sugiro usar
-WhatIfou-Confirmno cmdlet Remove-Item