How could I have made this Powershell script that executes robocopy code better I am trying to get better at Powershell and was wondering if I could get some advice on how to make this code more efficient/cleaner/anything.

This code is intended to launch three batch windows running robocopy at the same time with unique log names.

I looked into using a foreach loop but wasn't sure how I could do that with so many unique variables. Thinking back, I probably could have imported each source, destination and log name into a CSV though but this may have been cleaner/better because it's all in one file instead.

# sets the files to exclude 
$defaultDirExclusionSet = '"$Recycle.Bin" "System Volume Information"'

# setting the filename perameter so that we have some timestamps.
$filedate = "$(get-date -format 'yyyy-MM-dd-HH-MM')"  # we can remove the HH and MM later... have this on now for testing.

#options for robocopy
$RoboOptions = "/MIR /COPY:DATSOU /ZB /R:1 /W:5 /XD $defaultDirExclusionSet /NP /TEE"

#main fileshares
$source = "\\server1\D$\Shares"
$destination = "\\server2\D$"
$logname = "fileshare"

#Citrix User Store Shares
$source1 = "\\server1\D$\CitrixUserStore"
$destination1 = "\\server2\E$\CitrixUserStore"
$logname1 = "ctxusrshare"

#Citrix User Redir Shares
$source2 = "\\server1\D$\CitrixUserRedir"
$destination2 = "\\server2\E$\CitrixUserRedir"
$logname2 = "ctxusrredir"


##### launch all at once.  fix the first line before using.

Start-Process robocopy.exe -ArgumentList "$($source) $($destination) $($RoboOptions.split(' ')) /log+:c:\robocopylogs\$($logname)_$($filedate).txt"
Start-Process robocopy.exe -ArgumentList "$($source1) $($destination1) $($RoboOptions.split(' ')) /log+:c:\robocopylogs\$($logname1)_$($filedate).txt"
Start-Process robocopy.exe -ArgumentList "$($source2) $($destination2) $($RoboOptions.split(' ')) /log+:c:\robocopylogs\$($logname2)_$($filedate).txt"
If you don't want an external csv file,
use an internal one - simulated with a here string and ConvertFrom-Csv
in a date format specifier upper case MM are for month, minutes are lower case mm
the following script has several variable names shorted to keep line length down.
the final command is only echoed / commented out.




## Q:\Test\2019\08\23\sf_226703.ps1
# sets the files to exclude 
$defaultDirExclusionSet = '"$Recycle.Bin" "System Volume Information"'

#options for robocopy
$RoboOpts = "/MIR /COPY:DATSOU /ZB /R:1 /W:5 /XD $defaultDirExclusionSet /NP /TEE"

# simulate external csv with a here string and ConvertFrom-Csv
$RCJobs = @'
desc,src,dst,log
"main fileshares","\\server1\D$\Shares","\\server2\D$","fileshare"
"Citrix User Store Shares","\\server1\D$\CitrixUserStore","\\server2\E$\CitrixUserStore","ctxusrshare"
"Citrix User Redir Shares","\\server1\D$\CitrixUserRedir","\\server2\E$\CitrixUserRedir","ctxusrredir"
'@ -split '\r?\n' | ConvertFrom-Csv

$RCJobs | Format-Table -Auto

##### launch all at once.  fix the first line before using.
foreach($J in $RCJobs){
    Write-Host -Fore Green "Starting job $($J.desc)"
    $Args = '"{0}" "{1}" {2} /log+:"C:\robocopylogs\{3}_{4:yyyy-MM-dd-HH-mm}.txt"' -f `
           $J.src,$J.dst,$RoboOpts,$J.log,(Get-Date)
    "Start-Process robocopy.exe -ArgumentList $Args"
    #Start-Process robocopy.exe -ArgumentList $Args  
}


Sample output without executing RoboCopy.

&gt; Q:\Test\2019\08\23\sf_226703.ps1

desc                     src                          dst                          log
----                     ---                          ---                          ---
main fileshares          \\server1\D$\Shares          \\server2\D$                 fileshare
Citrix User Store Shares \\server1\D$\CitrixUserStore \\server2\E$\CitrixUserStore ctxusrshare
Citrix User Redir Shares \\server1\D$\CitrixUserRedir \\server2\E$\CitrixUserRedir ctxusrredir


Starting job main fileshares
Start-Process robocopy.exe -ArgumentList "\\server1\D$\Shares" "\\server2\D$" /MIR /COPY:DATSOU /ZB /R:1 /W:5 /XD "$Recycle.Bin" "System Volume Information" /NP /TEE /log+:"C:\robocopylogs\fileshare_2019-08-23-20-59.txt"
Starting job Citrix User Store Shares
Start-Process robocopy.exe -ArgumentList "\\server1\D$\CitrixUserStore" "\\server2\E$\CitrixUserStore" /MIR /COPY:DATSOU /ZB /R:1 /W:5 /XD "$Recycle.Bin" "System Volume Information" /NP /TEE /log+:"C:\robocopylogs\ctxusrshare_2019-08-23-20-59.txt"
Starting job Citrix User Redir Shares
Start-Process robocopy.exe -ArgumentList "\\server1\D$\CitrixUserRedir" "\\server2\E$\CitrixUserRedir" /MIR /COPY:DATSOU /ZB /R:1 /W:5 /XD "$Recycle.Bin" "System Volume Information" /NP /TEE /log+:"C:\robocopylogs\ctxusrredir_2019-08-23-20-59.txt"