FileMoveAutomation.ps1


<#PSScriptInfo
 
.VERSION 1.0.0
 
.GUID a3c517fb-afae-470c-bdf9-773737bd3af7
 
.AUTHOR jadedarchitect
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS File Copy Move Automation
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
 
 
.DESCRIPTION
 Moves files created today with specified naming convention from specified source share to target share, unless they already exist. Tracks success and failure, and notifies on any failures, as well as task completion.
 
#>
 

Param()


##############################
# Copies files from Source to Target. Count total files, and moved files - then sends task completion notification.
# Only copies files with today's date as CREATION DATE. Use $_.LastWriteTime.Date in place of $_.CreationTime.Date if you need files MODIFIED today.
#Define file name pattern, end with * if file names end randomly. For example, Define "*.extensiontype" for all files of a specific extension.
#Example grabs all files beginning with "FileExample" and any trailing characters or extensions. Use "*" for ALL files in directory matching today's date.
$FilePattern = "FileExample*"
##############################
#Set your variables!
$Source = "\\SourceServer\Share"
$Target = "\\DestinationServer\Share"
$SMTPServerA = "Server.domain.com"
$Sender = "Sender@Domain.com"
$Recipient = "Recipient@Domain.com"
$Server = "ServerNameThisIsRunningOn"
$JobName = "Name your scheduled task"
$Subject = "$JobName on $Server - Notification"



# Get date
$today = (Get-Date).Date

#Loops through files for those matching criteria of file name pattern and creation date and copies to target directory
$FileList = Get-ChildItem -Path $Source -Include $FilePattern -File -Recurse | Where-Object { $_.CreationTime.Date -eq $today } 
$FileCount = (Get-ChildItem -Path $Source -Include $FilePattern -File -Recurse | Where-Object { $_.CreationTime.Date -eq $today } | Measure-Object).Count
$MovedCount = 0
$FailedCount = 0
foreach ($File in $FileList) {
    $Name = $File.Name
    $targetFile = Join-Path -Path $Target -ChildPath $Name
    #Check for file in container, if it exists set failure variables and continue.
    if (Test-Path -Path $targetFile -PathType Leaf) {
        $FailedCount++
        ############
        #Disable this section if you expect a large number of failures. I am NOT responsible for your spamming yourself or anyone else!
        $SMTPBody1 = "$JobName on $Server - Error - $Name exists in $Target - File not moved. Email sent for visibility"
        Send-MailMessage -SmtpServer "$SMTPServerA" -From $Sender -To $Recipient -Subject $Subject -Body "$SMTPBody1"
        ############
    }
    #Copy if not already present
    else {
        $_ | Copy-Item -Destination $Target -Force
        $MovedCount++
    }
    { Continue }

}
#Send completion notification
$SMTPBody2 = "Task $JobName has completed on $Server. Of $FileCount files matching criteria in $Source, $MovedCount were moved to $Target and $FailedCount were not moved"
Send-MailMessage -SmtpServer "$SMTPServerA" -From $Sender -To $Recipient -Subject $Subject -Body "$SMTPBody2"