CHDS
Clément HugéClément Hugé
28.1.2020

Moving Files and Subfolders Using PowerShell

We usually come across situations where we need to move files and folders to another directory say for archival or some other purpose.

Powershell square

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
) 

try {           
    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  
    } 

} catch {       
    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:

68b5f9faca6a91f3ebf2a7b9_BI_1.jpeg68b5f9fa48fe788a225b258a_BI_2.jpeg68b5f9fac3bbcd227d702011_BI_3.jpeg

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

68b5f9fae20f30193a96c358_BO_1.jpeg

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

68b5f9facbe6b05e7c4fb021_EP_1.jpeg

After Execution of MoveFile.ps1

Input Folder and Subfolders:

68b5f9faabf9441cc97d4314_AI_1.jpeg

68b5f9fa52edd4499ae2cad3_AI_2.jpeg

68b5f9fa9d3cb83604a2610f_AI_3.jpeg

Output Folder and Subfolders:

68b5f9fac51a06d81219cc22_AO_1.jpeg

68b5f9fa7660c35eff596a92_AO_2.jpeg

68b5f9fae9fcf74bb08892a0_AO_3.jpeg

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