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: Install DotNet 3.5 – the Easy Way

$package="dotnet3.5"# Install Chocolateyif (!(Get-Command choco.exe -ErrorAction SilentlyContinue)) {Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))}#…

PowerShell: Trigger Azure AD Sync on Remote Server as a Domain Admin

# Trigger-AD-Sync.ps1# Requirements: https://blog.kimconnect.com/powershell-install-rsat/## This script does these things:# a. Obtain Domain Admin credential# b.…

ASCII Characters

DEC OCT HEX Symbol HTML Number Description 32 040 20     Space 33 041…