rename files down to 3 characters then delete them if they are 6 mo old

  Kiến thức lập trình

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

jorge google is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

LEAVE A COMMENT