Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources: Active Threads
Viewing all articles
Browse latest Browse all 8411

Process element in list one by one?

$
0
0

Hello,

I have got a script that I would like to use for removing directories where their modified date is older than x number of days.  I have managed to convert the DirectoryInfo object to a list of string objects by using the select-object -ExpandProperty however the script tries to work all directory names at once rather than each individually.  Here is the script, please can you help me out as I cannot find out how to work on each folder at a time:

#Define Variables

$Now = Get-Date
$Days = "90"
$LastWrite = $Now.AddDays(-$Days)
$server = '\\myserver'


foreach ($item in $server){
$path = get-childitem -path "$item\c$\documents and settings\" | Where {$_.psIsContainer -eq $true} | Where {$_.LastWriteTime -le "$LastWrite"} | Select-Object -ExpandProperty Name
Remove-Item "$item\c$\documents and settings\$path"
}

Many thanks for any help you can provide Smile


Viewing all articles
Browse latest Browse all 8411

Latest Images

Trending Articles



Latest Images