How to use Powershell to recursively go through the home folder
Want to loop through the Home directory and find a folder named tempDocuments. The physical folder structure is like D:\Home\username1\TempDocuments, D:\Home\username2\TempDocuments, D:\Home\username3\TempDocuments.  Need to loop through each users TempDocuments and delete folders older than x days.  I have a script to delete files but cannot figure out how to create a variable and store the username then evaluate the path?
August 23rd, 2013 4:36pm

Try This ( i use Powershell 3.0)

$HomeTempFolders = Get-ChildItem -Path D:\home -Recurse -Directory | Where-Object {(Split-Path $_ -leaf) -eq "TempDocuments"} | Select-Object -ExpandProperty Fullname
This code Remove all Folders that older 10 days (lastwritetime) if you remove the whatif Parameter.

$HomeTempFolders = Get-ChildItem -Path D:\home -Recurse -Directory | Where-Object {(Split-Path $_ -leaf) -eq "TempDocuments"} | Select-Object -ExpandProperty Fullname
foreach ($Folder in $HomeTempFolders) {
    Get-ChildItem -Path $Folder -Recurse -Directory | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-10)} | Remove-Item -Force -WhatIf 
}


!!!
The script does what you want! But remember.
It delete all Folders older than 10 days, independed of whether there are newer files contains in the folder!
!!!

Best regards
brima





Free Windows Admin Tool Kit Click here and download it now
August 23rd, 2013 4:48pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics