开发者

powershell script delete folders regular expression

开发者 https://www.devze.com 2023-01-13 21:07 出处:网络
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 t

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号