Skip to content

Instantly share code, notes, and snippets.

@alex-berezan
Created January 20, 2016 05:20
Show Gist options
  • Save alex-berezan/c48e4a24909df9ce51eb to your computer and use it in GitHub Desktop.
Save alex-berezan/c48e4a24909df9ce51eb to your computer and use it in GitHub Desktop.
Sample powershell script for fancy copy of files from one folder to another
function Copy-FolderContentsWithProgress
{
[CmdletBinding()]
Param(
[string] $SourceFolder,
[string] $DestinationFolder
)
$Files = Get-ChildItem -Path $SourceFolder -Recurse
$N = $Files.Length
$I = 0
$Files | ForEach-Object {
$PercentComplete = $I * 100 / $N
$CurrentFileName = $_.FullName
Write-Progress -Activity "Copying from $SourceFolder to $DestinationFolder" `
-PercentComplete $PercentComplete `
-CurrentOperation $CurrentFileName `
-Status "Copying"
Write-Verbose "Copying $CurrentFileName ..."
Copy-Item -Path $CurrentFileName -Destination $DestinationFolder -Force
Write-Verbose "Ok"
$I++
$PercentComplete = $I * 100 / $N
Write-Progress -Activity "Copying from $SourceFolder to $DestinationFolder" `
-Status "Ok" `
-PercentComplete $PercentComplete `
-CurrentOperation $CurrentFileName
}
Write-Progress -Activity "Copying from $SourceFolder to $DestinationFolder" `
-Completed $true
}
# examples:
# Copy-FolderContentsWithProgress -SourceFolder "D:\temp\" -DestinationFolder "D:\temp2\" -Verbose
# Copy-FolderContentsWithProgress -SourceFolder "D:\VirtualMachines\Windows Server 2016 Nano\Virtual Hard Disks\" -DestinationFolder "D:\temp2\" -Verbose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment