PowerShell script to copy items to a folder and then move to another folder based on input

Problem:

This is to solve a manual process whereby I copied files from a local disk to a network share.  Then moved the files within the local disk to a backup folder.  I wanted the script to prompt the user for the destination drive.

Solution:

Add-Type -AssemblyName Microsoft.VisualBasic
$Drive = @("M:\","N:\","R:\","W:\")
$DriveDest = $Drive | Out-GridView -Title "Choose a network destination" -Passthru
$SourceFolder = "F:\Ready\Movies\"
$TargetFolderM = "F:\Ready_To_Move\M"
$TargetFolderN = "F:\Ready_To_Move\N"
$TargetFolderR = "F:\Ready_To_Move\R"
$TargetFolderW = "F:\Ready_To_Move\W"

Get-ChildItem -Path $SourceFolder -Recurse | 
    ForEach-Object {
If ($DriveDest -eq 'M:\' )  {
        Copy-Item -Path $_.Fullname -Destination M:\ -Recurse
        Move-Item -Path $_.Fullname -Destination $TargetFolderM -force

        }
If ($DriveDest -eq 'N:\' )  {
        Copy-Item -Path $_.Fullname -Destination N:\ -Recurse
        Move-Item -Path $_.Fullname -Destination $TargetFolderN -force
        }
If ($DriveDest -eq 'R:\' )  {
        Copy-Item -Path $_.Fullname -Destination R:\ -Recurse
        Move-Item -Path $_.Fullname -Destination $TargetFolderR -force
        }
If ($DriveDest -eq 'W:\' )  {
        Copy-Item -Path $_.Fullname -Destination W:\ -Recurse
        Move-Item -Path $_.Fullname -Destination $TargetFolderW -force
        }
}

Leave a Reply

Your email address will not be published. Required fields are marked *