Created
January 20, 2016 05:20
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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