Share via


Powershell - Copy a list of files to multiple servers and backup exisiting files

Question

Wednesday, June 6, 2012 4:59 PM

Hello..

I would like to have assistance from Powershell gurus..

My scenario is; I want to automate a task to copy some .dll files from one location to multiple servers but before copying those files over to those servers; I'd like to take a backup for those same files which will be overwritten on to a local server where these .dll files will be copied from

My preference would be that the script should read 1 file (.txt or .csv) which will contain the name of those files that need to be copied over and get backedup and then read another file which will contain the list of servers.

example

File to copy = filename.dll (this filename.dll should be read from a .txt or .csv file which could contain more than 1 file)

Server to copy to = server1, server2, server3 (this should be read from a .csv file)

From the local server; Read filename(s).dll from the file, the servername from the server file and before copying the new files over, copy those exact same files from the shared folders on those servers and paste them on the local server by creating a folder as the servername and paste the file in there.

Then copy the new files over to those servers from the list and overwrite the existing files.

I really appreciate your time and effort in this

Thanks

All replies (11)

Tuesday, June 12, 2012 2:42 AM ✅Answered | 10 votes

Hi,

This is a very simple powershell script to transfer a file to a list of computes contained in a text file.

#Point the script to the text file$Computers = Read-Host "Enter Location Of TXT File"# sets the varible for the file location ei c:\temp\ThisFile.exe$Source = Read-Host "Enter File Source"# sets the varible for the file destination$Destination = Read-Host "Enter File destination (windows\temp)"# displays the computer names on screenGet-Content $Computers | foreach {Copy-Item $Source -Destination \\$_\c$\$Destination

Or:

$a = Get-Content "D:\Scripting\Srvlist.txt" foreach ($i in $a) {$files= get-content "D:\Scripting\filelist.txt"foreach ($file in $files){Copy-Item $file -Destination \\$i\C$\temp -force}}

Regards,

Yan Li

Yan Li

TechNet Community Support


Friday, September 12, 2014 3:08 PM

Hi ,

There is a way to get log from the second script? I mean, log which files were copied?


Friday, September 12, 2014 3:13 PM | 1 vote

Hi ,

There is a way to get log from the second script? I mean, log which files were copied?

You can use the -PassThru switch:

http://ss64.com/ps/copy-item.html

If you have additional questions, please start your own thread.

Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)


Friday, September 12, 2014 5:33 PM

thank You! And sorry about that.


Friday, September 12, 2014 5:49 PM

Cheers, you're welcome.

Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)


Friday, October 24, 2014 2:29 PM

Thanks this helped a lot!

But is there a way to read a text file which contains the source paths of multiple files which you want to copy to multiple computers?

Using the same process which allows you to put multiple computers in a text file. 

Thanks in advance, 

Tom 


Wednesday, February 25, 2015 5:59 AM

Hello,

How about copying MULTIPLE FILES to MULTIPLE SERVERS.

Above script is all about copying single file to all servers.

In my scenario, i do not want to copy a folder instead i would like to pickup few from it. i.e more than 1.

regards,

Dharanesh,


Wednesday, February 25, 2015 1:27 PM

Hello,

How about copying MULTIPLE FILES to MULTIPLE SERVERS.

Above script is all about copying single file to all servers.

In my scenario, i do not want to copy a folder instead i would like to pickup few from it. i.e more than 1.

regards,

Dharanesh,

Please start your own thread. You'll need to post your code and your errors.

Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)


Wednesday, December 9, 2015 4:32 PM

How about a write out to log? Do you know the syntax for that so I can then automate the line compare with the original when the count is high?

Patrick Burwell, VP, iQor, Inc.


Wednesday, December 9, 2015 4:36 PM

How about a write out to log? Do you know the syntax for that so I can then automate the line compare with the original when the count is high?

Patrick Burwell, VP, iQor, Inc.

See my post right above yours.


Thursday, July 27, 2017 12:56 PM | 1 vote

Hi Yan Li,

Why is it asking for Input when you run the script? I just want it to pick up the servers list and copy the files from the source. When I run the script it is asking which server, file location etc... I am not understanding.

Is that the way it should be? I just want one script that will copy all the files from a specific folder on a server and copy it to multiple servers from the servers.txt file.



AA2913