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: Office 365 Bulk Licensing, Changing IDs, Enabling POP3

Connect to Office 365 # Office 365 Global Admin Credential$username="O365globalAdmin"$password=ConvertTo-securestring "PASSWORDHERE" -AsPlainText -Force$cred = New-Object…

Script to Backup Exchange Server

REM exchange.txtD:\Exchange2010\mailbox\TempDatabaseD:\Exchange2010\mailbox\Mailbox-Database-20150403D:\Exchange2010\mailbox\Public Folder Database 1998117930 REM exchangebackup.batecho offnet use S: \\FILESERVER01\backups\EXCHANGE-2010REM -------------------------------------------------------REM PARSE THE DATE…

PowerShell: Connect to Office 365 Exchange Online

function disconnectExchangeOnline{ <# manually check sessions PS C:\Windows\system32> Get-PSSession Id Name ComputerName ComputerType State ConfigurationName…