Search code examples
powershellfilecopystartup

Using a Variable to Copy Files From One Location to Multiple Using Powershell


So i have a file that i want to transfer from one location to multiple computers on my network using powershell, this is what i have so far.

robocopy "\\PTFGW-061403573\c$\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup" 
"\\$onlineComputers\c$\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup" /mir /r:1 /o:0

when i run it it says the location is not found, and i can confirm that the variable holds all of the online computers within it. any ideas?


Solution

  • Zizzay,

    Here's some code that should do what you want. It puts the copy-item command in a TRY/Catch construct to catch and report any errors in the copying process. I assumed that the last item on your source (Startup) is a folder, thus the need for the Recurse argument in the $CIArgs. If it is not a folder remove the Recurse line. I also broke up your paths to cut down on repetition and make things easier to change. Note: I tested this on a Peer-to-Peer LAN, should work on a Domain based LAN.

    $onlineComputers = "Computer1","ComputerN"
    $FSBase = "C$\ProgramData\Microsoft\Windows\Start Menu\Programs"
    
    $OnlineComputers  | 
       ForEach-Object {
    
     Try {
            $CIArgs = 
              @{Path        = "\\PTFGW-061403573\$FSBase"
                Destination = "\\$_.\$FSBase\Startup"
                Force       = $True
                Recurse     = $True
                ErrorAction = 'Stop'}
            Copy-Item @CIArgs
           }
           Catch {
                 Write-Host @"
    Could not write to: $_.
    "@
           } #End Catch
    
    } #End ForEach...
    

    HTH