I have a need to archive files older that x days on file servers. I also needed this script to be something that a limited PowerShell admin could use. This is what I came up with.
$Search = Read-Host "Enter the volume to search (U:)"
$Archive = Read-Host "Enter the archive location (Y:)"
$ArchivePath = Read-Host "Enter the new directory to be created for your archive"
$Old = Read-Host "Enter the age of files for archive (x days old)"
$FArcPath = $Archive+$ArchivePath
set-location $Archive
new-item -path $ArchivePath -type directory -ea silentlycontinue
set-location $Search
$Files = get-childitem -recurse | where-object {((get-date) – ($_.Lastwritetime)).days -ge $Old}
foreach ($File in $Files) {
$MoveDirectory = (Join-Path -path $FArcPath -childpath $File.DirectoryName.SubString($pwd.path.length))
new-item $MoveDirectory -type directory -ea silentlycontinue
$File | move-item -destination {join-path -path $FArcPath -childpath $File.Fullname.Substring($pwd.path.length)}
}