Home » General » POWERSHELL SCRIPT COPY FILES USING ROBOCOPY

POWERSHELL SCRIPT COPY FILES USING ROBOCOPY

POWERSHELL SCRIPT COPY fILES USING ROBOCOPYPOWERSHELL SCRIPT COPY fILES USING ROBOCOPY

Please find powershell code which will assist you in copying data from source location to destination using robocopy.exe.

This script will display progress  of copy along with time remaining in readable format.

 

System Requirements:

  • A windows machine
  • Powershell (with windows 8 and 2012 above no need to install also with windows 2008).
  • Run script as administrator.

Please find code below:

RCOPY.PS1

$src,$dst=0
Clear-Host
Add-Type -AssemblyName System.Windows.Forms

$frm=New-Object System.Windows.Forms.Form
$frm.size=New-Object System.Drawing.Size(400,300)
$frm.text=”Robocopy – Jeevan Bobba”
$frm.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual
$frm.Location=new-object  System.Drawing.Point(800,0)
$frm.MaximizeBox=$false
$lbl1=new-object System.Windows.Forms.Label
$lbl1.text=”Enter Source below”
$lbl1.top=50
$lbl1.left=50
$lbl1.Width=200

$lbl2=New-Object System.Windows.Forms.Label
$lbl2.text=”Enter Destination below”
$lbl2.top=120
$lbl2.left=50
$lbl2.Width=200

$sourcetext=new-object System.Windows.Forms.TextBox
$sourcetext.top=80
$sourcetext.left=50
$sourcetext.Width=300

$desttext=New-Object System.Windows.Forms.TextBox
$desttext.top=150
$desttext.left=50
$desttext.Width=300

$copybutn=new-object System.Windows.Forms.Button
$copybutn.top=180
$copybutn.left=150
$copybutn.text=”Start  Robocopy”
$copybutn.Width=100

$frm.Controls.add($lbl1)
$frm.Controls.add($lbl2)
$frm.Controls.add($sourcetext)
$frm.Controls.add($desttext)
$frm.Controls.add($copybutn)

$exec={
if(($sourcetext.text -ne “”) -and ($desttext.text -ne “”))
{
if((test-path $sourcetext.text.ToString()) -and (test-path $desttext.text.ToString()))
{
$src = Get-ChildItem $sourcetext.text -recurse -ErrorAction SilentlyContinue | Measure-Object -property length -sum
$src=$src.sum

$runcmd=”robocopy”
$info=new-object System.Diagnostics.ProcessStartInfo
$info.FileName=”robocopy.exe”
$info.Arguments=”$($sourcetext.text.ToString()) $($desttext.text.ToString()) /E /ZB /DCOPY:T /COPYALL /R:1 /W:1  /LOG:c:\Robocopy.log  ”
$info.UseShellExecute=$false
$info.CreateNoWindow=$false
$cmd1=[System.Diagnostics.Process]::Start($info)
$clk=[system.diagnostics.stopwatch]::startNew()
while(Get-Process -Name $cmd1.ProcessName -ErrorAction SilentlyContinue)
{
$clksec=[math]::Round(($clk.ElapsedMilliseconds/1000))
$dst= Get-ChildItem $desttext.Text -recurse -ErrorAction SilentlyContinue | Measure-Object -property length -sum
$dst=$dst.sum
[double]$exttme=[math]::Round(($clksec/$dst)*$src)
try
{
$dd=[timespan]::FromSeconds($exttme)
}
catch
{}
$dd=”$($dd.Hours):$($dd.Minutes):$($dd.Seconds)”
$prog=$dst/$src*100
$prog=[math]::Round($prog)
#write-host “Total $($prog) % done” -ForegroundColor Green -BackgroundColor White
write-progress -Activity “copying from $($sourcetext.text) to $($desttext.text) and Estimated time left in hh:mm:ss $($dd)” -PercentComplete $prog -Status “$prog % completed”

}
$clk.Stop()
write-host “All copied,check c:\robocopy.log and total time taken $($clksec) seconds” -ForegroundColor Green
$frm.dispose()
}
else
{write-host “check source or  destination path ” -ForegroundColor DarkRed -BackgroundColor White
$frm.dispose()
}
}
else
{
write-host “check source or  destination path ” -ForegroundColor DarkRed -BackgroundColor white
#[void]$frm.close()
$frm.dispose()
}

}
$copybutn.add_click($exec)
$rt=$frm.ShowDialog()
$frm.Dispose()

Leave a Reply

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

Name *
Email *
Website