Need help with simple powershell command copy using foreach

Question:

I’m new to powershell and this question will prove that point. I’m trying a simple task from the command line where I have a txt file containing filenames separated by semicolons like…

I’m trying to run a command which will parse this file, split the contents by semicolon, and then run a copy command for each file to a desired directory.

Here is the command I’m running:

gc myfile.txt |% {$_.split(";") | copy $_ "C:\my\desired\directory"}

But I’m getting an error like this for each item in the list…

Answer:

Resist the urge to make one-liners, especially when you’re starting out. That said, the problem is you need to pipe the split content to another ForEach-Object.

Try this:

Source:

Need help with simple powershell command copy using foreach by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply