I’ve pieced together this command from multiple places on the web, (most from here, thank you.)
Get-ChildItem -Path “\$pcc$users” -recurse -force | where {$_.LastWriteTime -le $(get-date).Adddays(-180)} | Rename-Item -NewName {
$_.name.substring(0, [Math]::Min($_.BaseName.length, 3))} | rm -confirm:$false -ErrorAction ignore
I want it to rename all files to the first 3 characters, then delete them if they are older than 180. The command mostly works. But doesn’t delete the files after wards. Once the command is done I can’t do it again because the original files were modified so now they are less than 180 days old. Is there something I can change, or can I run a second command to delete all files that are 3 characters in length only?
Thank you.
New contributor
1