powershell script delete folders regular expression
I've written a Powershell script to delete subfolders within a given folder whose name starts with either 0 or 1. This 开发者_StackOverflowscript seems to work only for non-empty folders. I want it to delete the inner contents too. Is there any switch that makes it possible? Also, for some files, I get an error about not having enough permissions whereas the script runs as an administrator.
$srcFolder = "C:\Documents and Settings\setup\Desktop\Temp\"
$folderList = Get-Childitem $a | Where-Object {$_.Name -match "^[01]"}
foreach( $folder in $folderList)
{
$tempFolder = $srcFolder + $folder;
Remove-Item $tempFolder
}
Any ideas?
Regards,
Sujeet Kuchibhotla
You can simplify this quite a bit:
Get-ChildItem $srcFolder | Where {$_.PSIsContainer -and ($_ -match '^[01]')} |
Remove-Item -Recurse -WhatIf
精彩评论