'Powershell: Copy files to another folder by appending filenames as parent folder

I am trying to copy a bunch of files from a folder to another remote folder by renaming the files as the source folder name. The script I wrote works, but it is creating folders by appending the folder name as the parent, instead of copying the actual files itself.

$source = 'C:\Users\testserver\Desktop\WSVC1'

$destination = 'C:\Users\testserver\Documents\Powershell test'


$files = Get-ChildItem -File $source -Filter *.txt -Recurse -Force 


foreach ($file in $files) {

    $newPath = Join-Path -Path $destination -ChildPath $file-$(Split-Path -Path $source -Leaf)

    Copy-Item -Path $source -Destination $newPath
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source