6

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!

3 Answers 3

16

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.