Friday, June 2, 2017

Using Get-ChildItem to list only file (and not folder) names and copy them to a destination folder


Using Get-ChildItem to list only file (and not folder) names and copy them to a destination folder

Assume you have a folder structure as below and you want to copy the files in each subfolder to a single folder Eg: D:\Temp

 
D:
|Files
          |-Folder A
          |                    |Sub Folder A
          |                                               |File1.txt
          |                                               |File2.txt
          |                                               |File3.txt
          |-Folder A
          |                    |Sub Folder A
          |                                              |File4.txt
          |                                              |File5.txt
          |                                              |File6.txt
          |+Folder A
          |+Folder A

Below PowerShell script will help you to achieve the same . This will copy all the files from :D\Files recursively to the destination folder.

 
clear
Get-ChildItem -Path "D:\Files " -Recurse | ? {$_.Directory } | % {
Copy-Item -Path $_.FullName -Destination "D:\Temp "
}

No comments:

Post a Comment