tags:

views:

38

answers:

2

I've written a Powershell script to delete subfolders within a given folder whose name starts with either 0 or 1. This script 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

A: 

I saw that I changed the variable names in the code and forgot to replace it in all places.

-sk

A: 

You can simplify this quite a bit:

Get-ChildItem $srcFolder | Where {$_.PSIsContainer -and ($_ -match '^[01]')} | 
    Remove-Item -Recurse -WhatIf
Keith Hill