scapegoat17
scapegoat17

Reputation: 5821

Copy multiple files using PowerShell

I am currently learning how to use PowerShell. I was wondering if it would be possible for someone to tell me how to copy multiple text files from multiple sub-directories using the command line portion of PowerShell. I know how to do this in regular CMD and it looks something like this:

for /f "delims=" %F in ('dir \*.txt /s/b') do copy "%~F" "C:\test\" /Y

But obviously this does not work in PowerShell and needs a good bit of tweaking. Any help would be much appreciated. Thanks!

Upvotes: 6

Views: 24462

Answers (3)

user7610
user7610

Reputation: 28751

To pass in multiple file names, you can list them comma-delimited in the command.

mkdir a, b, c
cp a,b c

This results in both directories a and b being copied into directory c.

PS C:\Users\Vitorio> dir c

    Directory: C:\Users\Vitorio\c

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          07.11.2021    18:13                a
d----          07.11.2021    18:13                b

Upvotes: 0

user2196798
user2196798

Reputation: 39

This is a very simple line. test-servers.txt is the file containing your list of servers/machines (one entry per line).

Get-Content C:\Temp\test-servers.txt | ForEach-Object { Invoke-Command -ScriptBlock{Copy-Item -Path C:\Temp\resources\* -Destination ("\\" + $_ + "\C$\<path-of-interest>\")}}

NOTE: $_ is a variable holding each entry in your test-servers.txt file recursively. NOTE: This specific example copies all files in the "path" to a list of "remote" servers/machines.

Upvotes: 0

scapegoat17
scapegoat17

Reputation: 5821

I was actually able to figure it out. This is how i did it:

get-childitem -path "SOURCE\PATH" -filter *.txt -recurse | copy-item -destination "DESTINATION\PATH" 

Thanks anyway!

Upvotes: 16

Related Questions