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

PowerShell: Microsoft Dynamics Update All Organizations

# updateCrmOrgs.ps1 function updateCrmOrgs{ try{ Add-PSSnapin Microsoft.Crm.Powershell $servers=Get-CrmServer $highestVersion=($servers.Version|measure-object -Maximum).Maximum $lowVersionServers=$servers|?{[version]$_.Version -lt [version]$highestVersion} if($lowVersionServers){ write-warning…

PowerShell: Increase CPU Count or Memory of VMs via Virtual Machine Manager

# IncreaseCpuandRamViaVMM.ps1 # User Input Variables $vmNames=@( 'TESTWINDOWS', 'TESTWINDOWS2' ) $vmmServer=$env:computername $setCpuCount=8 $setDynamicMemory=$false $dynamicMemoryMinimumGB='2GB' $dynamicMemoryMaximumGB='16GB'…

Add a Domain Group to Local Administrators Group

$checkGroup="Administrators" $addMember="KIMCONNECT\Desktop Admins" # Dynamic Credential $who = whoami if ($who.substring($who.length-2, 2) -eq "-admin"){$username=$who;} else…
Other project: DragonCoin.com