Posted On December 19, 2019

PowerShell: Copying Only Files From a Folder

kimconnect 0 comments
blog.KimConnect.com >> Codes >> 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 *

Related Post

Basic CSS: Prioritize One Style Over Another

<style>body {background-color: black;font-family: monospace;color: green;}</style><h1>Hello World!</h1>

PowerShell: Inter-Domain SMB Shares Mounting

# interDomainFileCopy.ps1 # Note: this is a quick and dirty method. Password security must be…

PowerShell: Add Accounts into Local Administrators Group

# addAccountToLocalAdmins.ps1# Requires: PowerShell Version 4.0+$accountToAdd='ServerAdmins'$groupName="Administrators"$servers=@( "CONCU1", 'CONCU2', 'CONCU100', 'CONCU80665', 'CONCU6547354', 'CONWHAT989', 'CONCU3')$servers|%{ $session=new-pssession $_…