Set-Location : Cannot process argument because the value of argument "path" is null. Change the value of argument "path" to a non-null value.

Miguel Cuba 21 Reputation points
2021-05-07T20:38:45.95+00:00

Hello Everyone,
I was wondering if someone can help me with the following error:

Set-Location : Cannot process argument because the value of argument "path" is null. Change the value of argument "path" to a non-null value.

The script runs properly, it creates the folders and everything, but it always gets that error message.
I am also seeing some
0
1
2
3
As soon as I run the script, do anyone know why?

Here is the full script
Thank you

# Load required assemblies
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

# Drawing form and controls
$CreateFolder = New-Object System.Windows.Forms.Form
    $CreateFolder.Text = "Create Multiple Custodian Folders"
    $CreateFolder.Size = New-Object System.Drawing.Size(350,415)
    $CreateFolder.FormBorderStyle = "FixedDialog"
    $CreateFolder.TopMost = $true
    $CreateFolder.MaximizeBox = $false
    $CreateFolder.MinimizeBox = $false
    $CreateFolder.ControlBox = $true
    $CreateFolder.StartPosition = "CenterScreen"
    $CreateFolder.Font = "Segoe UI"


#======================== CASE NAME ========================#
# adding a label to my form
$label_message = New-Object System.Windows.Forms.Label
    $label_message.Location = New-Object System.Drawing.Size(20,8)
    $label_message.Size = New-Object System.Drawing.Size(100,15)
    $label_message.Text = "Case Name"
    $CreateFolder.Controls.Add($label_message)    
# CaseName    
    $CaseName = New-Object System.Windows.Forms.TextBox
    $CaseName.Location = New-Object System.Drawing.Size(20,30)
    $CaseName.Size = New-Object System.Drawing.Size(300,25)
    $CaseName.ScrollBars = "Vertical"
    $CreateFolder.Controls.Add($CaseName)


#======================== DROPBOX ========================#
$label_messageCombobox = New-Object System.Windows.Forms.Label
    $label_messageCombobox.Location = New-Object System.Drawing.Size(20,60)
    $label_messageCombobox.Size = New-Object System.Drawing.Size(100,15)
    $label_messageCombobox.Text = "Pick a Server"
    $CreateFolder.Controls.Add($label_messageCombobox)    

$DropdownBox = New-Object System.Windows.Forms.ComboBox
    $DropdownBox.Location = New-Object System.Drawing.Size(20,80)
    $DropdownBox.Size = New-Object System.Drawing.Size(300,15)
    $DropdownBox.Height = 200
    $Dropdownbox.DropDownStyle = "DropDownList"
    $CreateFolder.Controls.Add($DropdownBox)

    $Servers = @("Lab Machine 40","Lab Machine 45","Lab Machine 50","Lab Machine 55")


    foreach($Server in $Servers){
        $DropdownBox.Items.Add($Server)
    }

Function Get-Server{
    $SelectedServer = $DropdownBox.SelectedItem.ToString()

    if($SelectedServer -eq "Lab Machine 50") {
        $LabServer = Set-Location "\\Server50\K$"
    }
    elseif($SelectedServer -eq "Lab Machine 55") {
        $LabServer = Set-Location "\\Server55\K$"
    }
    elseif($SelectedServer -eq "Lab Machine 40") {
        $LabServer = Set-Location "\\Server40\K$"
    }
    elseif($SelectedServer -eq "Lab Machine 45") {
        $LabServer = Set-Location "\\Server45\K$"
    }


}


#======================== INPUTBOX ========================#
$label_message2 = New-Object System.Windows.Forms.Label
    $label_message2.Location = New-Object System.Drawing.Size(20,110)
    $label_message2.Size = New-Object System.Drawing.Size(100,15)
    $label_message2.Text = "Custodian Names"
    $CreateFolder.Controls.Add($label_message2)    

# Inputbox    
    $Inputbox = New-Object System.Windows.Forms.TextBox
    $Inputbox.Multiline = $True;
    $Inputbox.Location = New-Object System.Drawing.Size(20,130)
    $Inputbox.Size = New-Object System.Drawing.Size(300,200)
    $Inputbox.ScrollBars = "Vertical"
    $CreateFolder.Controls.Add($Inputbox)


# add a button ti create folder
$button_ClickMe = New-Object System.Windows.Forms.Button
    $button_ClickMe.Location = New-Object System.Drawing.Size(20,340)
    $button_ClickMe.Size = New-Object System.Drawing.Size(240,32)
    $button_ClickMe.TextAlign = "MiddleCenter"
    $button_ClickMe.Text = "Create Folders"
    $button_ClickMe.Add_Click({Get-Server})
    $button_ClickMe.Add_Click({

    If ($CaseName.TextLength -eq 0){
        [System.Windows.MessageBox]::Show('Please Enter a Case Name')
    }

    elseif ($Inputbox.TextLength -eq 0){
        [System.Windows.MessageBox]::Show('Please enter 1 custodian name')
    }

    else {

    Set-Location $LabServer
    New-Item $CaseName.Text -type directory
    Start-Sleep -Seconds 5
    Set-Location ($LabServer.Text + $CaseName.Text)

    #$button_ClickMe.Text = "Folders were created"

    ForEach ($Folder in $Inputbox.lines) {
        New-Item $Folder -type directory

          }


    }




    })
    $CreateFolder.Controls.Add($button_ClickMe)


# show form
$CreateFolder.Add_Shown({$CreateFolder.Activate()})
[void] $CreateFolder.ShowDialog()
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-05-07T22:42:35.487+00:00

    Hi @Miguel Cuba ,

    If I read your script right you are setting the variable $LabServer in a function.

    If you call the function in a script, the script is the parent scope and the function is the child scope. Variables in a child scope are by default not available in the parent scope: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.1#parent-and-child-scopes
    This means by default the variables in functions are not available in the script.
    It's possible to change the scope of a variable. Please try this:

    Function Get-Server{  
        $SelectedServer = $DropdownBox.SelectedItem.ToString()  
         
        if($SelectedServer -eq "Lab Machine 50") {  
            $script:LabServer = Set-Location "\\Server50\K$"  
        }  
        elseif($SelectedServer -eq "Lab Machine 55") {  
            $script:LabServer = Set-Location "\\Server55\K$"  
        }  
        elseif($SelectedServer -eq "Lab Machine 40") {  
            $script:LabServer = Set-Location "\\Server40\K$"  
        }  
        elseif($SelectedServer -eq "Lab Machine 45") {  
            $script:LabServer = Set-Location "\\Server45\K$"  
        }  
         
         
    }  
    

    Here you can find a simple script for testing the scope of variables in script and functions:
    https://github.com/abaumgarten42/Useful_PSscripts/blob/main/variables-ScriptAndFunctions/variables-ScriptAndFunctions.ps1

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. Miguel Cuba 21 Reputation points
    2021-05-08T04:44:15.413+00:00

    Thank you for the response,

    I made the changes you suggested, but the issue persists. There is one more new error:

    Here are the Errors

    Set-Location : Cannot process argument because the value of argument "path" is null. Change the value of argument "path" to a non-null value.
    At C:\APPS\Scripts\CreateFolder - Test.ps1:104 char:9
    +         Set-Location $LabServer
    +         ~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Set-Location], PSArgumentNullException
        + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.SetLocationCommand
    
    New-Item : Cannot bind argument to parameter 'Path' because it is an empty string.
    At C:\APPS\Scripts\CreateFolder - Test.ps1:111 char:22
    +             New-Item $Folder -type directory
    +                      ~~~~~~~
        + CategoryInfo          : InvalidData: (:) [New-Item], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.NewItemCommand
    

    Thank you for all your help

    Here is the full script:
    # Load required assemblies
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

    # Drawing form and controls
    $CreateFolder = New-Object System.Windows.Forms.Form
        $CreateFolder.Text = "Create Multiple Custodian Folders"
        $CreateFolder.Size = New-Object System.Drawing.Size(350,415)
        $CreateFolder.FormBorderStyle = "FixedDialog"
        $CreateFolder.TopMost = $true
        $CreateFolder.MaximizeBox = $false
        $CreateFolder.MinimizeBox = $false
        $CreateFolder.ControlBox = $true
        $CreateFolder.StartPosition = "CenterScreen"
        $CreateFolder.Font = "Segoe UI"
    
    
    #======================== CASE NAME ========================#
    # adding a label to my form
    $label_message = New-Object System.Windows.Forms.Label
        $label_message.Location = New-Object System.Drawing.Size(20,8)
        $label_message.Size = New-Object System.Drawing.Size(100,15)
        $label_message.Text = "Case Name"
        $CreateFolder.Controls.Add($label_message)    
    # CaseName    
        $CaseName = New-Object System.Windows.Forms.TextBox
        $CaseName.Location = New-Object System.Drawing.Size(20,30)
        $CaseName.Size = New-Object System.Drawing.Size(300,25)
        $CaseName.ScrollBars = "Vertical"
        $CreateFolder.Controls.Add($CaseName)
    
    
    #======================== DROPBOX ========================#
    $label_messageCombobox = New-Object System.Windows.Forms.Label
        $label_messageCombobox.Location = New-Object System.Drawing.Size(20,60)
        $label_messageCombobox.Size = New-Object System.Drawing.Size(100,15)
        $label_messageCombobox.Text = "Pick a Server"
        $CreateFolder.Controls.Add($label_messageCombobox)    
    
    $DropdownBox = New-Object System.Windows.Forms.ComboBox
        $DropdownBox.Location = New-Object System.Drawing.Size(20,80)
        $DropdownBox.Size = New-Object System.Drawing.Size(300,15)
        $DropdownBox.Height = 200
        $Dropdownbox.DropDownStyle = "DropDownList"
        $CreateFolder.Controls.Add($DropdownBox)
    
        $Servers = @("Lab Machine 40","Lab Machine 45","Lab Machine 50","Lab Machine 55")
    
    
        foreach($Server in $Servers){
            $DropdownBox.Items.Add($Server)
        }
    
    Function Get-Server{
        $SelectedServer = $DropdownBox.SelectedItem.ToString()
    
        if($SelectedServer -eq "Lab Machine 50") {
            $script:LabServer = Set-Location "\\Server50\K$"
        }
        elseif($SelectedServer -eq "Lab Machine 55") {
            $script:LabServer = Set-Location "\\Server55\K$"
        }
        elseif($SelectedServer -eq "Lab Machine 40") {
            $script:LabServer = Set-Location "\\Server40\K$"
        }
        elseif($SelectedServer -eq "Lab Machine 45") {
            $script:LabServer = Set-Location "\\Server45\K$"
        }
    }
    
    #======================== INPUTBOX ========================#
    $label_message2 = New-Object System.Windows.Forms.Label
        $label_message2.Location = New-Object System.Drawing.Size(20,110)
        $label_message2.Size = New-Object System.Drawing.Size(100,15)
        $label_message2.Text = "Custodian Names"
        $CreateFolder.Controls.Add($label_message2)    
    
    # Inputbox    
        $Inputbox = New-Object System.Windows.Forms.TextBox
        $Inputbox.Multiline = $True;
        $Inputbox.Location = New-Object System.Drawing.Size(20,130)
        $Inputbox.Size = New-Object System.Drawing.Size(300,200)
        $Inputbox.ScrollBars = "Vertical"
        $CreateFolder.Controls.Add($Inputbox)
    
     #======================== BUTTON ========================#   
    # add a button ti create folder
    $button_ClickMe = New-Object System.Windows.Forms.Button
        $button_ClickMe.Location = New-Object System.Drawing.Size(45,340)
        $button_ClickMe.Size = New-Object System.Drawing.Size(240,32)
        $button_ClickMe.TextAlign = "MiddleCenter"
        $button_ClickMe.Text = "Create Folders"
        $button_ClickMe.Add_Click({Get-Server})
        $button_ClickMe.Add_Click({
    
        If ($CaseName.TextLength -eq 0){
            [System.Windows.MessageBox]::Show('Please Enter a Case Name')
        }
    
        elseif ($Inputbox.TextLength -eq 0){
            [System.Windows.MessageBox]::Show('Please enter 1 custodian name')
        }
    
        else {
            Set-Location $LabServer
            New-Item $CaseName.Text -type directory
            Start-Sleep -Seconds 5
            Set-Location ($LabServer.Text + $CaseName.Text)
            #$button_ClickMe.Text = "Folders were created"
    
            ForEach ($Folder in $Inputbox.lines) {
                New-Item $Folder -type directory
            }
    
    
      #======================== YES OR NO MESSAGE ========================#    
        $UserResponse= [System.Windows.Forms.MessageBox]::Show("Do you want to continue? YES for a new Case, NO to Exit" , "Create Folders", 4)
    
            if ($UserResponse -eq "YES" ) 
            {
                $CaseName.Text = ''
                $Inputbox.Text = ''
            } 
            else 
            { 
                [System.Windows.Forms.Application]::Exit()
            } 
    
        }
    
       })
    
        $CreateFolder.Controls.Add($button_ClickMe)
    
    
    # show form
    $CreateFolder.Add_Shown({$CreateFolder.Activate()})
    [void] $CreateFolder.ShowDialog()
    
    0 comments No comments

  3. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-05-08T08:01:39.17+00:00

    Hi @Miguel Cuba ,

    what is the output of $LabServer.Text and $CaseName.Text ?
    Both variables you are combing in line 104.

    A variable doesn't have the property Text. The value of a variable is in the property Value.

    You could test this:

    Get-Variable LabServer | Select *  
    Get-Varibale CaseName | Select *  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  4. Anonymous
    2021-05-10T05:11:02.027+00:00

    Hi,

    The Set-Location cmdlet generates no output by default so the variable $LabServer is null. Since the location is set in the Add_Click you should just set the value of $LabServer in the function Get-Server. The 0,1,2,3 are generated by the line $DropdownBox.Items.Add($Server). To remove it you can send the output to null using the Out-Null cmdelt.

    foreach($Server in $Servers){  
        $DropdownBox.Items.Add($Server) | Out-Null  
    }  
      
    Function Get-Server{  
        $SelectedServer = $DropdownBox.SelectedItem.ToString()  
      
        if($SelectedServer -eq "Lab Machine 50") {  
            $script:LabServer = "\\Server50\K$"  
        }  
        elseif($SelectedServer -eq "Lab Machine 55") {  
            $script:LabServer = "\\Server55\K$"  
        }  
        elseif($SelectedServer -eq "Lab Machine 40") {  
            $script:LabServer = "\\Server40\K$"  
        }  
        elseif($SelectedServer -eq "Lab Machine 45") {  
            $script:LabServer = "\\Server45\K$"  
        }  
    }  
    

    And the variable $folder should be checked before creating the folders.

    Set-Location ($CaseName.Text)  
    ForEach ($Folder in $Inputbox.lines) {  
            if ($Folder){  
                New-Item $Folder -type directory  
            }  
        }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  5. Miguel Cuba 21 Reputation points
    2021-05-10T14:13:17.37+00:00

    Thank you so much for both of your help!
    I was able to remove the 1,2,3,4, and remove the PATH thing.
    I added -PassThru after the Set-Location

        if($SelectedServer -eq "Lab Machine 50") {
            $Script:LabServer = Set-Location "\\Server50\K$" -PassThru
    

    But now I am getting the following:

    New-Item : Cannot bind argument to parameter 'Path' because it is an empty string.
    At C:\APPS\Scripts\CreateFolder - Test.ps1:112 char:22
    +             New-Item $Folder -type directory
    +                      ~~~~~~~
        + CategoryInfo          : InvalidData: (:) [New-Item], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.NewItemCommand
    

    The error seems to be in

    New-Item $Folder -type directory
    

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.