Powershell script to copy mutiple files in a folder to remote severs and validate the file was replaced successfully

Posted on 2011-10-05
Last Modified: 2012-05-12
I'm new to powershell and would like to build a script that will accomplish the following:

1. Replace files on multiple servers with files in a file share.
2. After copy, verify the the version of replaced files.

Can someone show me some templates or point me to right direction? Thanks in advance for your help.
Question by:lo_oscar
    LVL 35

    Expert Comment

    loop through a list of server names, then for each use "net use" command to map a drive, and then use copy-item to copy the files from your share
    after copying delete the mapped drive

    $cred = Get-Credential()
    $networkCred = $cred.GetNetworkCredential()
    net use \\servername\test\ $networkCred.Password /USER:$networkCred.UserName
    Copy-Item -Path C:\Test\ -Destination \\servername\test\ -Credential $cred
    net use \\server\test\ /delete

    Open in new window

    LVL 35

    Expert Comment


    Author Comment

    how do I add multiple destinations in this script?

    $source = 'C:\folder1\'
    $destination = '\\server\Folder1\'
    $logProgress = 'c:\temp\copyLog.txt'

    get-childitem $source -recurse | foreach {
    copy $_.fullname $destination -Force -Recurse -errorAction silentlyContinue
    if($? -eq $false){echo "$($_.fullname) did not copy ok to $destination" | out-file -append $logProgress}
    {write-output "$($_.fullname) copied OK to $destination" | out-file -append $logProgress}
    LVL 67

    Accepted Solution

    I assume you do not need the credentials stuff as shown by YZlat, and then you do not need the net use either.

    Your code would
    a) go thru the complete folder structure,
    b) and start copying this structure each time
    so you would have too many copy operations doing exactly the same.

    You already allow copy-item to copy the complete tree. You just need to apply that to more than one destination.
    $source = 'C:\folder1\'
    $destination = '\\server\Folder1\','\\server2\Folder1'
    $logProgress = 'c:\temp\copyLog.txt'
    $destination | % {
      write-output "=====`nCopy to: $_`n=====`n"
      copy-item $_.fullname %_ -Force -Recurse -errorAction Continue
    } | out-file -append $logProgress

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
    How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now