Copying files but not all the folders, just the one parent/relative folder in which contains the files.
Folder and Files structure:
ExcelData (Folder)
a. File1.xlsx
b. File2.xlsx
c. File3.xlsx
d. File14.xlsx
e. File15.xlsx
Code I'm using:
$sourcePath = '\\171.168.10.98\One_Data'
$destinationPath = 'C:\Temp'
$files = Get-ChildItem -Path $sourcePath -Recurse
$filecount = $files.count
$i=0
Foreach ($file in $files) {
$i++
Write-Progress -activity "copying files..." -status "($i of $filecount) $file" -percentcomplete (($i/$filecount)*100)
# Determine the absolute path of this object's parent container. This is stored as a different attribute on file and folder objects so we use an if block to cater for both
if ($file.psiscontainer) {$sourcefilecontainer = $file.parent} else {$sourcefilecontainer = $file.directory}
# Calculate the path of the parent folder relative to the source folder
$relativepath = $sourcefilecontainer.fullname.SubString($sourcepath.length)
# Copy the object to the appropriate folder within the destination folder
copy-Item $file.fullname ($destinationPath + $relativepath)
}