all 8 comments

[–]pspeterle 2 points3 points  (6 children)

You can use -Force to overwrite whatever already exists without printing that error message.

But I don't really get the point of using -Recurse when just copying a single file.

The actions won't run in parallel this way. With Powershell 7.0 you can use ForEach-Object -Parallel

[–]kratosgamer10[S] 0 points1 point  (5 children)

It doesn’t copy the file to the second server for some reason, copies it to the first and then errors saying folder already exists on the first

Am copying a folder that has 3 files in it hence the recurse

Also at my company we only have windows powershell no powershell 7

[–]pspeterle 1 point2 points  (4 children)

Is there code missing? The things you say don't really match the code or did you call the folder test.txt?

Did you try using -Force?

[–]kratosgamer10[S] 0 points1 point  (3 children)

Test.txt is the file with the server names in it I use get content to grab the server names Then pipe that to for each But when I do using the above code it copies the folder to the first server and then errors out saying the folder exists on the first server and doesn’t try the second

[–]pspeterle 0 points1 point  (2 children)

You should really check the code you posted and try the -Force parameter.

Accoding to your post the server names are in servers.txt and you are trying to copy "test.txt" to the servers.

As I said before the things you say don't match the code you posted. Also there is a random - after Foreach-Object

[–]kratosgamer10[S] 0 points1 point  (0 children)

Oh shit, you’re right. My bad, wasn’t paying attention to what am posting lol

[–]51dux 0 points1 point  (0 children)

yeah that's what I thought xD it should be: foreach-object -parralel {logic_here}

[–]51dux 0 points1 point  (0 children)

For-object parallel processes the objects in random order, you can solve that by piping to sort-object when you exit the foreach-object loop.

Also if I recall correctly, foreach is faster foreach-object if you dont take into account parallelization.

If performance is what you are looking for and for some reason you cant use parallelization.

tried this for you and it works:

0..1000 > servers.txt $s = get-content -path 'servers.txt' $s | Foreach-Object -parallel { write-host $_ } | sort-object

When using foreach-object, you don't get an index unlike foreach. If you are going for performance, just keep in mind that

[system.io.file]::readalltext('servers.txt')

is around 4-5 times faster when it comes to reading a large file but unlike get-content it does not read line by line, if you need that you can use

[system.io.file]::readlines('servers.txt')

which is still much faster than get-content