We usually come across situations where we need to move files and folders to another directory say for archival or some other purpose. There are many scripting languages which can be used for it but we are going to see here the power of PowerShell to do such kind of tasks.

The solution here involved is creating a power shell script which accepts source folder, destination folder and noOfDays as parameter. It will check files which are older than noOfDays and move it to specified destination folder from source folder along with creation of subfolders inside destination folder in case those are not already created.

Create a PowerShell script file say MoveFile.ps1 and put below code in it.

param (

           [parameter(Mandatory = $true)] [string] $source,

           [parameter(Mandatory = $true)] [string] $destination, 

           [parameter(Mandatory = $true)] [int] $NoOfDays                  




            Get-ChildItem -Path $source -Recurse -Force |

            Where-Object { $_.psIsContainer } |

            ForEach-Object { $_.FullName -replace [regex]::Escape($source), $destination } |

            ForEach-Object {   if(!(Test-Path -Path $_ )){

                                                                                                $null = New-Item -ItemType Container -Path $_



            Get-ChildItem -Path $source -Recurse -Force |

            Where-Object {  (-not $_.psIsContainer) -and ($_.lastwritetime -lt (Get-                         date).AddDays(-$NoOfDays)) }

           | Move-Item -Force -Destination { $_.FullName -replace [regex]::Escape($source),  $destination  }




        Write-Host "$($MyInvocation.InvocationName): $_"


The above code accepts mandatory parameters source, destination and NoOfDays which need to be passed while running this power shell script. These parameters made this script reusable as it can be used to move files and subfolders from any source folder to specified destination folder. Also the files older than current days can be also specified in NoOfDays parameter being passed.

 Example showing usage of this Script:

    Before execution of script MoveFile.ps1

Input Folder and Subfolders:

Output Folder and Subfolders: You can see below it is empty as of now.

 Execute MoveFile.ps1 to move files and subfolders from FileIn to FileOut which are older than 3 days using the below command in Windows Power Shell

After Execution of MoveFile.ps1

 Input Folder and Subfolders:


Output Folder and Subfolders:


 We can see the files which were older than 3 days in FileIn folder have been moved to FileOut and respective subfolders also created.