Copy files to subfolders using powershell

Razzi29 336 Reputation points
2022-12-02T19:30:59.2+00:00

Hi All, I am trying to figure out why the script line below does not work as what I am trying to do is to copy a bunch of text files to sub-folders under a folder above.. for example, I want to copy to c:\temp\plugins\ and then have the script copy to every sub-folder

Copy-Item -Path "C:\TEMP\source\" -Destination "C:\Temp\Plugins" -Recurse -Force -Filter "*.txt"

Windows for business | Windows Server | User experience | PowerShell
Windows for business | Windows Client for IT Pros | User experience | Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. S.Sengupta 24,551 Reputation points MVP
    2022-12-03T01:46:49.457+00:00

  2. MotoX80 36,291 Reputation points
    2022-12-03T16:43:12.78+00:00

    Given this...

    C:\Temp\source\Subfolder1\New OpenDocument Text.txt

    Then this command...

    Copy-Item -Path "C:\TEMP\source\" -Destination "C:\Temp\newfolder\" -Recurse -Force -Filter "*.txt"

    Will produce...

    C:\Temp\newfolder\source\Subfolder1\New OpenDocument Text.txt

    And this command....

    Get-ChildItem -Path "C:\TEMP\source\" -directory | foreach {Copy-Item -Path $_.fullname -Destination "C:\Temp\newfolder\" -Recurse -Force -Filter "*.txt"}  
    

    Will produce...

    C:\Temp\newfolder\Subfolder1\New OpenDocument Text.txt

    If you want something different, then you will need to explain what you are looking for.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.