How about adding text boxes to display the folder names and reference those. Note that I did not change how you processed the robocopy output since I wasn't quite sure what you were trying to do there. I also added quotes to the folders in case they contain any spaces.
#********************************************************************************
# Description: a scripted frontend that allows the user to copy files *
# *
# ******* *
# Notes * *
# ******* *
# Script name: RoboCopy *
# Author: Brogan Derrington *
# Date: May 1st, 2025 *
# Modified: May 5th, 2025 *
# *******************************************************************************
# ******************
# Function Section *
# ******************
function SourceFolder($initialDirectory="MyComputer")
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null
$srcFolder = New-Object System.Windows.Forms.FolderBrowserDialog
$srcFolder.Description = "Select the directory with the items to be copied"
$srcFolder.rootfolder = "MyComputer"
$srcFolder.selectedPath = $initialDirectory
if($srcFolder.ShowDialog() -eq "OK")
{
$SourceFolder += $srcFolder.selectedPath
}
return $SourceFolder
}
function DestinationFolder($initialDirectory="MyComputer")
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null
$dstFolder = New-Object System.Windows.Forms.FolderBrowserDialog
$dstFolder.Description = "Select the directory where you would like the items copied to"
$dstFolder.rootfolder = "MyComputer"
$dstFolder.selectedPath = $initialDirectory
if($dstFolder.ShowDialog() -eq "OK")
{
$DestinationFolder += $dstFolder.selectedPath
}
return $DestinationFolder
}
# *******
# Checks *
# *******
# makes sure the user selects a source folder
function Checks {
if ($SourceInput.text.trim() -eq "") {
[System.Windows.Forms.MessageBox]::Show("Error: Source folder cannot be null.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
return
}
if ((test-path $SourceInput.text) -eq $false) {
[System.Windows.Forms.MessageBox]::Show("Error: Source folder does not exist. `n" + $SourceInput.text, "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
return
}
$SourceFolder = '"{0}"' -f $SourceInput.text #add quotes
# makes sure the user selects a destination folder
if ($DestinationInput.text.trim() -eq "") {
[System.Windows.Forms.MessageBox]::Show("Error: Destination folder cannot be null", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
return
}
if ((test-path $DestinationInput.text) -eq $false) {
[System.Windows.Forms.MessageBox]::Show("Error: Destination folder does not exist. `n" + $DestinationInput.text, "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
return
}
$DestinationFolder = '"{0}"' -f $DestinationInput.text # add quotes
# Validation checks passed, run robocopy
write-host $SourceFolder
write-host $DestinationFolder
Robocopy $SourceFolder $DestinationFolder /MIR /NDL /NJH /NJS | %{$data = $_.Split([char]9); if("$($data[4])" -ne "") { $file = "$($data[4])"} ;Write-Progress "Percentage $($data[0])" -Activity "Robocopy" -CurrentOperation "$($file)" -ErrorAction SilentlyContinue; }
write-host "Robocopy complete."
}
##############################
## End of Functions Section ##
##############################
##########################
## Defining GUI Buttons ##
##########################
Add-Type -AssemblyName System.windows.Forms
$form = New-Object 'System.Windows.Forms.Form'
$form.AutoScaleDimensions = '8, 17'
$form.AutoScaleMode = 'Font'
$form.ClientSize = '700, 300'
$form.FormBorderStyle = 'FixedDialog'
$form.Margin = '5, 5, 5, 5'
$form.MaximizeBox = $False
$form.MinimizeBox = $False
$form.ControlBox = $False
$form.Name = 'RoboCopy'
$form.StartPosition = 'CenterScreen'
$form.Text = 'RoboCopy'
$form.TopMost = $True # force window to stay on top
$form.add_Load($form_Load)
# *********************
# Create Source button *
# *********************
$SourceButton = New-Object 'System.Windows.Forms.Button'
$SourceButton.Font = 'Calibri, 12.25pt'
$SourceButton.Location = '50, 20'
$SourceButton.Margin = '5, 5, 5, 5'
$SourceButton.Size = '150, 30'
$SourceButton.BackColor ="LightGray"
$SourceButton.ForeColor ="black"
$SourceButton.Text = '&Source Folder'
$SourceButton.UseCompatibleTextRendering = $True
$SourceButton.UseVisualStyleBackColor = $False
# *********************************
# Action for Source Folder button *
# *********************************
$SourceButton.Add_Click({
$SourceInput.Text = SourceFolder
})
$SourceButton.Show()#$button.Hide()
$form.Controls.Add($SourceButton)
# *********************
# Create Source folder input *
# *********************
$SourceInput = New-Object 'System.Windows.Forms.TextBox'
$SourceInput.Font = 'Calibri, 12.25pt'
$SourceInput.Location = '240, 20'
$SourceInput.Margin = '5, 5, 5, 5'
$SourceInput.Size = '350, 30'
$SourceInput.BackColor ="LightGray"
$SourceInput.ForeColor ="black"
$SourceInput.Show()#$button.Hide()
$form.Controls.Add($SourceInput)
# ***************************
# Create Destination button *
# ***************************
$DestinationButton = New-Object 'System.Windows.Forms.Button'
$DestinationButton.Font = 'Calibri, 12.25pt'
$DestinationButton.Location = '50, 100'
$DestinationButton.Margin = '5, 5, 5, 5'
$DestinationButton.Size = '150, 30'
$DestinationButton.BackColor ="LightGray"
$DestinationButton.ForeColor ="black"
$DestinationButton.Text = '&Destination Folder'
$DestinationButton.UseCompatibleTextRendering = $True
$DestinationButton.UseVisualStyleBackColor = $False
# *********************************
# Action for Source Folder button *
# *********************************
$DestinationButton.Add_Click({
$DestinationInput.Text = DestinationFolder
})
$DestinationButton.Show()#$button.Hide()
$form.Controls.Add($DestinationButton)
# *********************
# Create Destination folder input *
# *********************
$DestinationInput = New-Object 'System.Windows.Forms.TextBox'
$DestinationInput.Font = 'Calibri, 12.25pt'
$DestinationInput.Location = '240, 100'
$DestinationInput.Margin = '5, 5, 5, 5'
$DestinationInput.Size = '350, 30'
$DestinationInput.BackColor ="LightGray"
$DestinationInput.ForeColor ="black"
$DestinationInput.Show()#$button.Hide()
$form.Controls.Add($DestinationInput)
# ***************************
# Create Sync Date button *
# ***************************
$SyncDataButton = New-Object 'System.Windows.Forms.Button'
$SyncDataButton.Font = 'Calibri, 12.25pt'
$SyncDataButton.Location = '100, 200'
$SyncDataButton.Margin = '5, 5, 5, 5'
$SyncDataButton.Size = '300, 30'
$SyncDataButton.BackColor ="LightGray"
$SyncDataButton.ForeColor ="black"
$SyncDataButton.Text = '&Sync Data'
$SyncDataButton.UseCompatibleTextRendering = $True
$SyncDataButton.UseVisualStyleBackColor = $False
# *********************************
# Action for Sync Data button *
# *********************************
$SyncDataButton.Add_Click({
Checks
})
$SyncDataButton.Show()#$button.Hide()
$form.Controls.Add($SyncDataButton)
# ********************
# Create Exit button *
# ********************
$ExitButton = New-Object 'System.Windows.Forms.Button'
$ExitButton.Font = 'Calibri, 12.25pt'
$ExitButton.Location = '100, 250'
$ExitButton.Name = 'buttonExit'
$ExitButton.Size = '300, 30'
$ExitButton.BackColor ="LightGray"
$ExitButton.ForeColor ="black"
$ExitButton.Text = '&Exit Program'
$ExitButton.UseCompatibleTextRendering = $True
$ExitButton.UseVisualStyleBackColor = $False
# *************************
# Action for Exit button *
# *************************
$ExitButton.Add_Click({$form.Add_FormClosing({$_.Cancel=$false});$form.Close()})
$ExitButton.Show()#$button.Hide()
$form.Controls.Add($ExitButton)
# Disable other types of close/exit
$form.add_FormClosing({$_.Cancel=$true})
[void] $form.ShowDialog()