Tuesday, June 23, 2020

PowerShell using RoboCopy, Copy Folders/Content to another location;

PowerShell using RoboCopy, Copy Folders/Content to another location;

There are several options when it comes to copy files from source to destination directory. You can use native PowerShell cmdlet “copy-item” however, you will quickly realize it is super slow. Using RoboCopy is the way to go by and here is simple and effective script to get the job done.


<#    
  
 
.NOTES
#=============================================
# Script      : RoboCopy-Files-V1.ps1
# Created     : ISE 3.0 
# Author(s)   : CA-Casey.Dedeal 
# Date        : 06/23/2020 12:58:36 
# Org         : ETC Solutions
# File Name   : RoboCopy-Files-V1.ps1
# Comments    : Copy Files from source to destination
# Assumptions :
#==============================================
 
SYNOPSIS           : RoboCopy-Files-V1.ps1
DESCRIPTION        : Copy folders/content source to destination
Acknowledgements   : Open license
Limitations        : None
Known issues       : None
Credits            : None
 
.EXAMPLE
  .\RoboCopy-Files-V1.ps1
 
  MAP:
  -----------
  #(1)_.Adding Vars
  #(2)_.Define source and destination vars
  #(3)_RoboCopy Options
  #(4)_.Function to check/create destination folder
 
  Note : Take out /TEE option if you want silent mode
  $Options     = ("/B","/MIR", "/XJ", "/FFT", "/R:0", "/V", "/LOG:$LogFile")
 
#>
 
 
 
#(1)_.Define source and destination vars
$timestamp = (Get-Date -format 'dd-MMM-yyyy-HH-mm-ss-')
$fileName  = 'RoboCopy.LOG'
$logname   = $now+$Filename
 
#(2)_RoboCpy Options
$dataSource  = ("\\kckvdts107\c$\Users\$luser\Documents\S_Drive_Scripts_\PS_Scripts\")
$dataDestin  = ("\\kckvdts109\c$\Users\$luser\Documents\S_Drive_Scripts_\")
$LogFile     = ($dataDestin+$logname )
$Options     = ("/B","/MIR", '/TEE', "/XJ", "/FFT", "/R:0", "/V", "/LOG:$LogFile")
 
 
#(3)_.Function to check/create destination folder
Function Function-Check-Destination-Folder{
 
  [CmdletBinding()]
  param(
    [parameter(
     Mandatory         = $true,
     ValueFromPipeline = $true)]
      [string]$DestinationPath)
 
 Try{
 
 if (!(Test-Path -Path $DestinationPath))
 {
  New-Item -Type Directory -Path $DestinationPath -ErrorAction Stop | Out-Null
 }
 
}catch{
  
    $errormessage = $($PSItem.ToString())
    Write-Warning 'Error has occoured'
    Write-host 'Problem FOUND:' $errormessage -ForegroundColor Red -BackgroundColor Black
   }
}
 
#(4)_.Function-Check-Source Folder , Stop if not found
Function Function-Check-Source-Folder{
 
  [CmdletBinding()]
  param(
    [parameter(
     Mandatory         = $true,
     ValueFromPipeline = $true)]
      [string]$SourcePath)
 
 Try{
 
 if (!(Test-Path -Path $SourcePath))
 {
     Write-Host 'CANNOT locate (SOURCE) directory' -ForegroundColor White -BackgroundColor Red
     Write-Host 'Script will stop' -ForegroundColor Yellow
     break;
 
 }
 
}catch{
  
    $errormessage = $($PSItem.ToString())
    Write-Warning 'Error has occoured'
    Write-host 'Problem FOUND:' $errormessage -ForegroundColor Red -BackgroundColor Black
   }
}
 
#(5)_.Check source path,Action:<STOP>, if it does not exist
Function-Check-Source-Folder -SourcePath $dataSource
 
#(6)_.Create Destination,action:<CREATE>, if it does not exist
Function-Check-Destination-Folder -DestinationPath $dataDestin
 
#(7)_Start Robocopy with pre-defined options
 
write-host '++++++++++++++++++++++++++++++++++++++++++++++++++++'
Write-host 'Starting RoboCopy now' -ForegroundColor Green
robocopy $dataSource $dataDestin $Options 
 

 

Azure Solutions Architect
AWS Certified Cloud Practitioner
Azure Certified Security Engineer Associate
https://simplepowershell.blogspot.com
https://cloudsec365.blogspot.com
https://msazure365.blogspot.com
https://twitter.com/Message_Talk

 


No comments:

Post a Comment

Microsoft M365 F3 licensing Limitations and Confusion

If you are working in regulated environment you could be dealing with F3 license for some of your users and I am sure you have read MS licen...