Search code examples
powershellscriptingpowershell-3.0copy-item

Copy same file to multiple different folders PowerShell


I want to copy one file logo.png to different multiple folders. As of now I am doing like this

Get-Childitem "D:\OrgIcon" -Recurse -Include "*logo.png" | 
Copy-Item -Destination "D:\LoginPage"

Get-Childitem "D:\OrgIcon" -Recurse -Include "*logo.png" | 
Copy-Item -Destination "D:\HomePage"

Get-Childitem "D:\OrgIcon" -Recurse -Include "*logo.png" | 
Copy-Item -Destination "D:\AboutUs"

Is there any way to make it single command?

I have seen this but looks different.


Solution

  • I am not aware of one single command to achieve what you are trying to do. As pointed out in the link you posted, you can achieve your goal with one line of code, but this involves piping.

    "D:\LoginPage", "D:\HomePage", "D:\AboutUs" | ForEach-Object { Get-Childitem "D:\OrgIcon" -Recurse -Include "*logo.png" | Copy-Item -Destination $_}
    

    In the first part you just list your destinations as strings and separated by comma.

    The second part will execute the code within the braces { } for each destination.

    Note: $_ stands for the data currently in the pipeline, so on each iteration $_ will be replaced with your destination.

    I guess if this is something you need to do regularly with different destinations, sources and filenames you could always write a script file that accepts input for it's parameters, but that's a topic for another question.

    Hope this is useful.