PowerShell: Copying Only Files From a Folder

Function (snippet):

function copyOnlyFiles{
param(
[Parameter(Mandatory=$true)][string]$sourceFolder,
[Parameter(Mandatory=$true)][string]$destinationFolder
)
$totalSizeBytes=0;
# Get-ChildItem "C:\Temp"|?{!$_.PSIsContainer}|%{robocopy "C:\Temp" "C:\TempCopy" $_}
# Get-ChildItem $sourceFolder|?{!$_.PSIsContainer}|%{robocopy $sourceFolder $destinationFolder $_}
# New-Item -ItemType Directory -Force -Path $destinationFolder | Out-Null
#$duration=measure-command {Get-ChildItem $sourceFolder|?{!$_.PSIsContainer}|Copy-Item -Destination $destinationFolder}
$duration=measure-command {
$filesOnly=Get-ChildItem $sourceFolder|?{!$_.PSIsContainer}
$filesOnly|%{$totalSizeBytes+=$_.Length;write-host $_;robocopy $sourceFolder $destinationFolder $_}
}
$totalsizeGb=$totalSizeBytes/1GB
$totalMinutes=[math]::round($duration.TotalMinutes,2)
$gbPerHour=[math]::round($totalsizeGb/(($duration.TotalMinutes)/60),2)
write-host "-------------------------------------------------`r`n$([math]::round($totalsizeGb,2)) GB completed in $totalMinutes minutes. Speed was $gbPerHour GB/Hour."
}

Sample Output:

PS C:\Windows\system32> copyOnlyFiles -sourceFolder c:\iso -destinationFolder C:\isocopy
New Text Document - Copy (2).txt
New Text Document - Copy (3).txt
New Text Document - Copy (4).txt
New Text Document - Copy (5).txt
New Text Document - Copy (6).txt
New Text Document - Copy (7).txt
New Text Document - Copy (8).txt
New Text Document - Copy (9).txt
New Text Document - Copy.txt
New Text Document.txt
Windows_Server_2016_Datacenter_EVAL_en-us_14393_refresh.ISO
-------------------------------------------------
4.76 GB completed in 0.42 minutes. Speed was 927.63 GB/Hour.

Leave a Reply

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