Deleting old files and folders within each user's directory

I’m trying to use a script which I downloaded from the Microsoft Script Repository and have already tested the syntax on a single user’s directory. However, I need to use it to process ALL user directories and yet not delete the user directories themselves. So the logic I’m trying to use is that all subfolders within the container “\Server1\shared_BizHub_Users” should be passed into the script named deleteold.ps1. I’m still learning PowerShell so I’m pretty sure I’ve got some syntax errors in both of my two options to solve this problem.

the immediate child objects user the path \Server1\shared_BizHub_Users folder is a folder for each user in the enterprise. For example,



In both Option 1 and Option 2 I’m fairly certain that i’m not passing the variable for the -FolderPath parameter for deleteold.ps1 correctly. Any help would be greatly appreciated! When I ran deleteold.ps1 and manually specified the full UNC path to a specific users folder, it ran as expected using the same variables shown below. I’m merely trying to pass the Folder Path as a variable using a loop.

Option 1
Get-ChildItem \\Server1\shared\_BizHub\_Users -force */* | where {$_.PSIsContainer} | foreach {deleteold.ps1 -FolderPath $_.DirectoryPath -FileAge 30 -logfile c:\temp\BizHub-DeleteOld.log -CompareCreateTimeLastModified -CleanFolders}
In Option 2
Option 2
$BizHubUserFolders = get-childitem -path "\\Server1\shared\_BizHub\_Users" | where-object {$_.Psiscontainer} | select-object FullName
deleteold.ps1 -FolderPath "$($BizHubUserFolders.FullName)" -FileAge 30 -logfile c:\temp\BizHub-DeleteOldFiles.log -CompareCreateTimeLastModified -CleanFolders

Please, when you post code format it as code using the code tag button (pre) right above the edit windows of the post editor. Thanks.

It’s always a bad idea to use a script or code you don’t fully understand. If I got you right you actually do not need a script. To delete files older than 30 days in a folder and all its subfolders you just need something like this:

$Path = ‘\Server1\shared_BizHub_Users’
$FileAgeDays = -30
$TimeSpamp = (Get-Date).AddDays($FileAgeDays)
Get-ChildItem -Path $Path -Recurse -Force -File |
Where-Object {$_.LastWriteTime -lt $TimeSpamp} |
Remove-Item -Force -Verbose 4>&1 |
Out-File -FilePath ‘c:\temp\BizHub-DeleteOldFiles.log’

Of course as always there’s a lot of room for improvements but it should bring you on the right track. :wink:

Explanation: the “4>&1” redirects the verbose output to the standard output and makes it “catchable” for the Out-File. Otherwise it would only show up in the console.

Hi Olaf,

Thank you for your response! Sorry for my post…it was my first. I will use the PRE tag in the future. I had run the deleteold.ps1 and it worked exactly as I expected but that was by me specifying the target folder. What I was trying to figure out is how to pass in the target folder as a variable within a loop. While I still don’t know how to do that, I did try the new code sample you provided and it seemed to work well also. So thank you for that!