robo copy script issue

Nagorb 20 Reputation points
2025-05-05T23:40:19.28+00:00

Hello everyone,

I hope everyone has had a great day. I was wondering if I might be able to get some help with a script I am working on (please note I am very new to PowerShell but have been doing a lot of research 😊). It is a simple gui for users to copy data. It is working except for my checks to make sure the user chose a source and destination location. I just can not figure out why my check will not work. Any help at all is greatly appreciated!

#********************************************************************************
# 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 ($SourceFolder -eq $null) {
    [System.Windows.Forms.MessageBox]::Show("Error: Remeber to chooser a source folder!", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)

}


# makes sure the user selects a destination folder

if ($DestinationFolder -eq $null) {
    [System.Windows.Forms.MessageBox]::Show("Error: Remeber to chooser a destination folder!", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)

}
}

SyncData

function SyncData {

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; }

}

##############################
## 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 = '500, 150'
$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({

$script:SourceFolder = SourceFolder

})

$SourceButton.Show()#$button.Hide()
$form.Controls.Add($SourceButton)

# ***************************
# Create Destination button *
# ***************************

$DestinationButton = New-Object 'System.Windows.Forms.Button'
$DestinationButton.Font = 'Calibri, 12.25pt'
$DestinationButton.Location = '300, 20'
$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({

$script:DestinationFolder = DestinationFolder

})

$DestinationButton.Show()#$button.Hide()
$form.Controls.Add($DestinationButton)


# ***************************
# Create Sync Date button *
# ***************************

$SyncDataButton = New-Object 'System.Windows.Forms.Button'
$SyncDataButton.Font = 'Calibri, 12.25pt'
$SyncDataButton.Location = '100, 65'
$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, 110'
$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()





Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2025-05-06T15:22:13.73+00:00

    At the very least, I'd start by making two simple changes to your code:

    if ($SourceFolder -eq $null) { should become if ($null -eq $SourceFolder) {

    And if ($DestinationFolder -eq $null) { should become if ($null -eq $DestinationFolder) {

    $SourceFolder and $DestinationFolder both appear to be array objects. Because PowerShell uses streaming when the object is on the left side of an operator, your code was comparing each element of those objects for equality with the value $null.

    I believe your intention was to compare the value of the variables $SourceFolder and $DestinationFolder (and not their contents) to the value $null.

    0 comments No comments

  2. MotoX80 36,291 Reputation points
    2025-05-07T19:13:08.0466667+00:00

    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()
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.