powershellpowershell-2.0powershell-3.0powershell-1.0

PowerShell script to move files and folders including subfolders from one location to another older than x days


I developed a PowerShell script, and it's working absolutely fine. The only challenge is the files in the subfolders are not getting moved to the destination.

get-childitem -Path "\\servername\location" |
    where-object {$_.LastWriteTime -lt (get-date).AddDays(-31)} |
    move-item -destination "C:\Dumps"

I am unable to customize the script further.


Solution

  • Use the -Recurse option on the Get-ChildItem command to get through to the files in the sub folders and then move each individually by piping the collection to Move-Item

    Get-ChildItem -Path "C:\Test" -Recurse |
      Where-Object {$_.LastWriteTime -lt (Get-date).AddDays(-31)} |
      Move-Item -destination "C:\Dumps"
    

    Here's a screenshot:

    screenshot