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?
Shift-3Connect With a Mentor Commented:
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

Is using PowerShell an absolute requirement?  This particular task would be easier in a batch script with the forfiles command.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.