YoavKlein
YoavKlein

Reputation: 2705

Moving or copying several files at once in Powershell

In bash, you can

cp filea fileb filec Folder

which will copy filea fileb and filec into Folder

For some reason, i couldn't find a way to do the same thing in Powershell.
Everywhere i looked, all i could find is just the possibility to use wildcards in order to move
a bunch of files from the same type like:

cp *.txt Folder

But that's not what i'm looking for. I'm looking for a way to copy several files
by their names in one command.

Does anybody knows a way to do that?

Upvotes: 1

Views: 85

Answers (2)

wasif
wasif

Reputation: 15470

A one liner to do:

@("file1","file2","filen") | % {Copy -Path $_ -Destination "destination"}

Upvotes: 0

derekbaker783
derekbaker783

Reputation: 9581

"I'm looking for a way to copy several files by their names in one command."


Here are three examples that can do what you want. The first one is closest to what you're looking for.

param(
    $files = @('filea', 'fileb', 'filec'),
    $dest = 'Folder'
)

Copy-Item -LiteralPath $files -Destination $dest -Verbose

$files | ForEach-Object { Copy-Item -Path $_ -Destination $dest -Verbose }

foreach ($file in $files) { Copy-Item -Path $file -Destination $dest -Verbose }

Upvotes: 2

Related Questions