Monday, June 23, 2025

Power shell - Copy files from one folder to another folder using

# Set source and destination paths

$sourcePath = "K:\AosService\PackagesLocalDirectory"

$destinationPath = "K:\AOSService_44"


# Get all XML files from source directory and subdirectories

$xmlFiles = Get-ChildItem -Path $sourcePath -Recurse -Filter *.xml


# Total number of files

$totalFiles = $xmlFiles.Count

$counter = 0


# Copy each file and show progress

foreach ($file in $xmlFiles) {

    $targetDir = $file.DirectoryName.Replace($sourcePath, $destinationPath)

    if (-not (Test-Path -Path $targetDir)) {

        New-Item -ItemType Directory -Path $targetDir -Force | Out-Null

    }


    Copy-Item -Path $file.FullName -Destination $targetDir -Force


    $counter++

    $percentComplete = [math]::Round(($counter / $totalFiles) * 100)

    Write-Progress -Activity "Copying XML files" -Status "$counter of $totalFiles copied" -PercentComplete $percentComplete

}


Write-Host "Completed copying $totalFiles XML files."