Copy Folders to a list of IP Addresses using Powershell

I created this script to copy a specific directory over the network to a bunch of computers. This is handy if you don’t have a deployment program like Altiris or anything else that can be automated with a click of a button or two.

The script will read a CSV file, which will contain the IP Address of the computer you want to copy the files over to. Optionally, you can have it set permissions to the destination folder if the computer is on (as seen in the script notes) after the copy is done. Afterwards, it will keep looping to the next entry in the list.

This will run in a loop and output information in a text file as a log. Pretty handy to see if it set it correctly or not. I do notice one issue which is that if you have a ton of files in the directory, it will log that in the log file. In the future, I might create separate log files per user folder to lessen the amount of data in the one log file.

This script has been tested on Windows 8.1. Make sure the execution policy for Powershell is set as Unrestricted while running this script and then turning it back on to Restricted.


Changelog:
1.0 -> 1.1
– Removed variable from $copyFrom, changed it to $source.. which is why it didn’t work. Whoops!
– Added an example in the notes for a few variables

Here is the two files as shown below:
CopyFilesIP.ps1

#############################
# CopyFilesIP.ps1
# Version 1.1
#
# Created by: Infinite Technica
# www.infinitetechnica.com
# Date: 2016/06/10
# 
# Licensed under Creative Commons Zero
# https://creativecommons.org/publicdomain/zero/1.0/
#############################


# Variables
# Change D:\ to any Drive letter you will put your CSV file at
$csv = Import-Csv 'D:\CopyFilesIP\IP.csv'
$currentDate = get-date -format yyyyMMdd
$currentRDate = get-date -format yyyy/MM/dd
$currentTime = get-date -format hh:mm:sstt
$outputName = "Logfile-" + $currentDate + ".txt"
#############################
# Change the Drive Path; Make sure you change D:\ if you don't want it at that path
$outputPath = "D:/$outputName"
#############################

#############################
# Directory you want to copy from (You can use UNC Paths or Actual Directory Paths)
# For source, it can be a UNC path (\\server\appfolder) or normal path (C:\appfolder or D:\testdir) etc.
# For copyTo it has to be a UNC related path (ie: d$\folder or C$\folder)
$source = "\\localhost\D$\TestDir"
$copyTo = "C$\TestDir"

#############################


# Creating Initial Log File
echo "Output log for Copy Files to IP Address Script" > $outputPath
echo "Date Ran: $currentRDate" >> $outputPath
echo "Time Ran: $currentTime" >> $outputPath
echo "================================================" >> $outputPath

# For Loop
foreach ($line in $csv) {
    # Variables
    $Address = $line.IP
   
    # Tests to see if computer is on or accepts pings
    $boolConnect = Test-Connection -ComputerName $Address -BufferSize 16 -Count 1 -Quiet

    echo "Working on $Address"

    if ($boolConnect -eq 'True') {

        # This will become \\ip\path (ex: \\192.168.1.2\c$\dir)
        $destination = "\\$Address\$copyTo"

        echo "-------------------------------" >> $outputPath
        echo "$Address is Online" >> $outputPath
        echo "-------------------------------" >> $outputPath
        # Copying file to computer
        robocopy $source $destination /MIR >> $outputPath


        # (OPTIONAL) Set Permissions for the folder
        icacls ($destination) /inheritance:d  >> $outputPath
        #icacls ($destination) /remove:g "Group1" /T >> $outputPath
        #icacls ($destination) /remove:g "Group2" /T >> $outputPath

    }
    else {
        echo "$Address is Offline" >> $outputPath
    }
    
    echo "-------------------------------" >> $outputPath
}

IP.csv

IP
192.168.1.2
192.168.1.3
192.168.1.4