move files older than 30day with wildcard in path

I am trying to do a script in powershell that will move files older that 30 days to a sub folder.  The trick is I have a folder called users that has a sub under it for each users network share as a sub of that is a scans folder that each user scans to from our network scanners.   Since each user has their own private scan folder we want to age the data in there but as a safety net we want to be able to move it to a delete folder first so if they have not saved it where it really belongs that it will be easy for them to get back.  Example I want to move files from d:\users\*\scans  to d:\users\*\scans\delete that are older than 30 days.   then give them another 30 days there before we auto delete them.    I got the delete working but am having trouble with the move.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Is using PowerShell an absolute requirement?  This particular task would be easier in a batch script with the forfiles command.
Ok, I went ahead and did it in PowerShell.

Paste the script below into a text file with a .ps1 extension.  Customize the value of the $mask variable on line 1 with the file mask of the folders to search.  Running the script will echo remove-item commands for any files in the \delete subfolders older than 60 days and move-item commands for any files in the scans folders older than 30 days.

Once you have tested it successfully, remove the -WHATIF switches from lines 10 and 11 to execute the commands.

$mask = "d:\users\*\scans"
$movecutoff = (get-date).AddDays(-30)
$delcutoff = (get-date).AddDays(-60)
foreach ($sub in get-childitem $mask | where-object {$_.PsIsContainer -eq "True"})
        $del = $sub.fullname + '\delete'
        new-item ($del) -force -type directory | out-null
        get-childitem $del | where-object {$_.PsIsContainer -ne "True" -and $_.LastWriteTime -lt $delcutoff} | remove-item -force -WHATIF
        get-childitem $sub.fullname | where-object {$_.PsIsContainer -ne "True" -and $_.LastWriteTime -lt $movecutoff} | move-item -destination $del -WHATIF

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.