Powershell Script to Copy Files from One Server to Another in the same Domain.

Purpose:
Simple script to copy files from one computer or server to another in the same domain. Example can
also email a simple report of files copied. Created to move some files hourly and only email when
something was moved.

Notes:
Will not work across different domains. Account used to run task from task manager must be a domain
user with enough rights on both systems to read and write.

#=====================================================================================================#
# Copyright 2014 Robert Stacks
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#
#=====================================================================================================#
# Original Author: Robert Stacks
# Original Script URL: https://randomtechminutia.wordpress.com/2014/02/03/copy-files-from-one-server-to-another-in-the-same-domain/
# Date: 2/3/2014
# Updated:
# Version: 1.0
#
# Purpose:
# Simple script to copy files from one computer or server to another in the same domain. Example can
# also email a simple report of files copied. Created to move some files hourly and only email when
# something was moved.
#
# Many of the user variables are set with $TRUE or $FALSE for on or off.
#
# Will not work across different domains. Account used to run task from task manager must be a domain
# user with enough rights on both systems to read and write.
#
# Update Notes:
#
#=====================================================================================================#
# Email Settings
$fromAddr = "From@domain.com" # Enter the FROM address for the e-mail alert
$toAddr = "To@domain.com" # Enter the TO address for the e-mail alert
$smtpsrv = "mail.domain.com" # Enter the FQDN or IP of a SMTP relay
# Files to Copy and where they are going
# Local Files should be in the formate of C:\Folder\* for Files only or C:\Folder to copy the folder as well
$Localfiles = "C:\Folder\*"
# Remote Path should be UNC path with admin share example \\hostname\c$\pathtocopyto
$Remotefiles = "\\hostname\c$\pathtocopyto"
#Enable or Disable Script functions
$EmailAlerts = $TRUE # Turn e-mail alerts on or off. $FALSE or 0 = off
$TestfromPrompt = $FALSE # Turn on output from command line. $FALSE or 0 = no output
$RemoveFilesafterCopy = $TRUE # Remove files after files have been copied over?
#===========================#
#Main Script #
#===========================#
#Get the names of the files we want to transfer
$files = Get-childitem "$Localfiles" |foreach { $_.Name}
if ($files -ne $null)
{
#Generates output to command line if Value = True
if ($TestfromPrompt -eq $TRUE)
{
Write-Host "These are the files being copied"
$files
}
#Copy files to remote computer
copy-item -path "$Localfiles" -Destination "$Remotefiles" -Recurse
#Remove Files if Value = True
if ($RemoveFilesafterCopy -eq $TRUE)
{
#Remove Files after they have been moved
Remove-Item "$Localfiles"
}
#Send Email Alert if Value = True
if ($EmailAlerts -eq $TRUE)
{
$date = Get-Date -Format g
Send-MailMessage -To $toAddr -From $fromAddr -Subject "$date Files Copied" -Body "The Following Files were moved: " + $files -SmtpServer $smtpsrv
}
}

Hrm I had a suggestion that was simple but the more I thought about it the more I realized it would be a bad idea. What we’ll need to do is change line 72 so its a foreach ($file in $files) loop and then as it copies each file append the date. Let me do a little code testing and I’ll post a solution.