Windows form is not chnaing his size

Hi all,

i created a script to update user details but not i want to add more option to the form that i created but looks like the form size and even title is not changing anyone can help?
this is the full script

# Load the Windows Forms assembly
Add-Type -AssemblyName System.Windows.Forms

# Create a new form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Edit User Info"
$form.Width = 1000
$form.Height = 500
$form.StartPosition = "CenterScreen"
#$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
# Create the form controls
$label1 = New-Object System.Windows.Forms.Label
$label1.Location = New-Object System.Drawing.Point(10, 20)
$label1.Size = New-Object System.Drawing.Size(100, 20)
$label1.Text = "Username:"
$form.Controls.Add($label1)

$usernameTextbox = New-Object System.Windows.Forms.TextBox
$usernameTextbox.Location = New-Object System.Drawing.Point(110, 20)
$usernameTextbox.Size = New-Object System.Drawing.Size(150, 20)
$form.Controls.Add($usernameTextbox)

$searchButton = New-Object System.Windows.Forms.Button
$searchButton.Location = New-Object System.Drawing.Point(270, 20)
$searchButton.Size = New-Object System.Drawing.Size(100, 20)
$searchButton.Text = "Search"
$searchButton.Add_Click({
    $username = $usernameTextbox.Text
    $user = Get-ADUser -Identity $username -Properties DisplayName, Description, EmailAddress, Office, Title, TelephoneNumber, msRTCSIP-PrimaryUserAddress
    if ($user) {
        $fullNameTextbox.Text = $user.DisplayName
        $descriptionTextbox.Text = $user.Description
        $emailTextbox.Text = $user.EmailAddress
        $officeTextbox.Text = $user.Office
        $titleTextbox.Text = $user.Title
        $phoneTextbox.Text = $user.TelephoneNumber
        $sipTextbox.Text = $user."msRTCSIP-PrimaryUserAddress"
        $updateButton.Enabled = $true
    }
    else {
        [System.Windows.Forms.MessageBox]::Show("User not found.")
    }
})

$form.Controls.Add($searchButton)

$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10, 50)
$label2.Size = New-Object System.Drawing.Size(100, 20)
$label2.Text = "Full Name:"
$form.Controls.Add($label2)

$fullNameTextbox = New-Object System.Windows.Forms.TextBox
$fullNameTextbox.Location = New-Object System.Drawing.Point(110, 50)
$fullNameTextbox.Size = New-Object System.Drawing.Size(260, 20)
$form.Controls.Add($fullNameTextbox)

$label3 = New-Object System.Windows.Forms.Label
$label3.Location = New-Object System.Drawing.Point(10, 80)
$label3.Size = New-Object System.Drawing.Size(100, 20)
$label3.Text = "Description:"
$form.Controls.Add($label3)

$descriptionTextbox = New-Object System.Windows.Forms.TextBox
$descriptionTextbox.Location = New-Object System.Drawing.Point(110, 80)
$descriptionTextbox.Size = New-Object System.Drawing.Size(260, 20)
$form.Controls.Add($descriptionTextbox)

$label4 = New-Object System.Windows.Forms.Label
$label4.Location = New-Object System.Drawing.Point(10, 110)
$label4.Size = New-Object System.Drawing.Size(100, 20)
$label4.Text = "Email Address:"
$form.Controls.Add($label4)

$emailTextbox = New-Object System.Windows.Forms.TextBox
# Load the Windows Forms assembly
Add-Type -AssemblyName System.Windows.Forms

# Create a new form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Edit User Info"
$form.Width = 400
$form.Height = 360
$form.StartPosition = "CenterScreen"

# Create the form controls
$label1 = New-Object System.Windows.Forms.Label
$label1.Location = New-Object System.Drawing.Point(10, 20)
$label1.Size = New-Object System.Drawing.Size(100, 20)
$label1.Text = "Username:"
$form.Controls.Add($label1)

$usernameTextbox = New-Object System.Windows.Forms.TextBox
$usernameTextbox.Location = New-Object System.Drawing.Point(110, 20)
$usernameTextbox.Size = New-Object System.Drawing.Size(150, 20)
$form.Controls.Add($usernameTextbox)

$searchButton = New-Object System.Windows.Forms.Button
$searchButton.Location = New-Object System.Drawing.Point(270, 20)
$searchButton.Size = New-Object System.Drawing.Size(100, 20)
$searchButton.Text = "Search"
$searchButton.Add_Click({
    $username = $usernameTextbox.Text
    if ([string]::IsNullOrEmpty($username)) {
        [System.Windows.Forms.MessageBox]::Show("Please enter a username.")
    }
    else {
        try {
            $user = Get-ADUser -Identity $username -Properties DisplayName, Description, EmailAddress, Office, Title, TelephoneNumber, msRTCSIP-PrimaryUserAddress, Enabled
            if ($user) {
                $fullNameTextbox.Text = $user.DisplayName
                $descriptionTextbox.Text = $user.Description
                $emailTextbox.Text = $user.EmailAddress
                $officeTextbox.Text = $user.Office
                $titleTextbox.Text = $user.Title
                $phoneTextbox.Text = $user.TelephoneNumber
                $sipTextbox.Text = $user."msRTCSIP-PrimaryUserAddress"
                $updateButton.Enabled = $true
                
                # Set the "Enable User" and "Disable User" checkboxes based on the user account status
                if ($user.Enabled) {
                    $enableCheckbox.Checked = $true
                    $disableCheckbox.Checked = $false
                }
                else {
                    $enableCheckbox.Checked = $false
                    $disableCheckbox.Checked = $true
                }
            }
            else {
                [System.Windows.Forms.MessageBox]::Show("User not found.")
            }
        }
        catch {
            [System.Windows.Forms.MessageBox]::Show("User not found")
        }
    }
})



$form.Controls.Add($searchButton)

$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10, 50)
$label2.Size = New-Object System.Drawing.Size(100, 20)
$label2.Text = "Full Name:"
$form.Controls.Add($label2)

$fullNameTextbox = New-Object System.Windows.Forms.TextBox
$fullNameTextbox.Location = New-Object System.Drawing.Point(110, 50)
$fullNameTextbox.Size = New-Object System.Drawing.Size(260, 20)
$form.Controls.Add($fullNameTextbox)

$label3 = New-Object System.Windows.Forms.Label
$label3.Location = New-Object System.Drawing.Point(10, 80)
$label3.Size = New-Object System.Drawing.Size(100, 20)
$label3.Text = "Description:"
$form.Controls.Add($label3)

$descriptionTextbox = New-Object System.Windows.Forms.TextBox
$descriptionTextbox.Location = New-Object System.Drawing.Point(110, 80)
$descriptionTextbox.Size = New-Object System.Drawing.Size(260, 20)
$form.Controls.Add($descriptionTextbox)

$label4 = New-Object System.Windows.Forms.Label
$label4.Location = New-Object System.Drawing.Point(10, 110)
$label4.Size = New-Object System.Drawing.Size(100, 20)
$label4.Text = "Email Address:"
$form.Controls.Add($label4)

$emailTextbox = New-Object System.Windows.Forms.TextBox
$emailTextbox.Location = New-Object System.Drawing.Point(110, 110)
$emailTextbox.Size = New-Object System.Drawing.Size(260, 20)
$form.Controls.Add($emailTextbox)

$label5 = New-Object System.Windows.Forms.Label
$label5.Location = New-Object System.Drawing.Point(10, 140)
$label5.Size = New-Object System.Drawing.Size(100, 20)
$label5.Text = "Office:"
$form.Controls.Add($label5)

$officeTextbox = New-Object System.Windows.Forms.TextBox
$officeTextbox.Location = New-Object System.Drawing.Point(110, 140)
$officeTextbox.Size = New-Object System.Drawing.Size(260, 20)
$form.Controls.Add($officeTextbox)

$label6 = New-Object System.Windows.Forms.Label
$label6.Location = New-Object System.Drawing.Point(10, 170)
$label6.Size = New-Object System.Drawing.Size(100, 20)
$label6.Text = "Title:"
$form.Controls.Add($label6)

$titleTextbox = New-Object System.Windows.Forms.TextBox
$titleTextbox.Location = New-Object System.Drawing.Point(110, 170)
$titleTextbox.Size = New-Object System.Drawing.Size(260, 20)
$form.Controls.Add($titleTextbox)

$label7 = New-Object System.Windows.Forms.Label
$label7.Location = New-Object System.Drawing.Point(10, 200)
$label7.Size = New-Object System.Drawing.Size(100, 20)
$label7.Text = "Phone Number:"
$form.Controls.Add($label7)

$phoneTextbox = New-Object System.Windows.Forms.TextBox
$phoneTextbox.Location = New-Object System.Drawing.Point(110, 200)
$phoneTextbox.Size = New-Object System.Drawing.Size(260, 20)
$form.Controls.Add($phoneTextbox)

$label8 = New-Object System.Windows.Forms.Label
$label8.Location = New-Object System.Drawing.Point(10, 230)
$label8.Size = New-Object System.Drawing.Size(100, 20)
$label8.Text = "SIP Address:"
$form.Controls.Add($label8)

$sipTextbox = New-Object System.Windows.Forms.TextBox
$sipTextbox.Location = New-Object System.Drawing.Point(110, 230)
$sipTextbox.Size = New-Object System.Drawing.Size(260, 20)
$form.Controls.Add($sipTextbox)
# Create the "Enable User" checkbox
$enableCheckbox = New-Object System.Windows.Forms.CheckBox
$enableCheckbox.Location = New-Object System.Drawing.Point(10, 260)
$enableCheckbox.Size = New-Object System.Drawing.Size(100, 20)
$enableCheckbox.Text = "Enable User"
$form.Controls.Add($enableCheckbox)

# Create the "Disable User" checkbox
$disableCheckbox = New-Object System.Windows.Forms.CheckBox
$disableCheckbox.Location = New-Object System.Drawing.Point(120, 260)
$disableCheckbox.Size = New-Object System.Drawing.Size(100, 20)
$disableCheckbox.Text = "Disable User"
$form.Controls.Add($disableCheckbox)

# Add event handlers for the checkboxes
$enableCheckbox.Add_CheckedChanged({
    if ($enableCheckbox.Checked) {
        $disableCheckbox.Checked = $false
    }
})

$disableCheckbox.Add_CheckedChanged({
    if ($disableCheckbox.Checked) {
        $enableCheckbox.Checked = $false
    }
})
$updateButton = New-Object System.Windows.Forms.Button
$updateButton.Location = New-Object System.Drawing.Point(100, 280)
$updateButton.Size = New-Object System.Drawing.Size(100, 30)
$updateButton.Text = "Update"
$updateButton.Enabled = $false
$updateButton.Add_Click({
    $username = $usernameTextbox.Text
    $fullName = $fullNameTextbox.Text
    $description = $descriptionTextbox.Text
    $email = $emailTextbox.Text
    $office = $officeTextbox.Text
    $title = $titleTextbox.Text
    $phone = $phoneTextbox.Text
    $sip = $sipTextbox.Text

    $user = Get-ADUser -Identity $username
    if ($user) {
        $params = @{
            'Identity' = $user
        }

        if ($fullName) {
            $params.Add('DisplayName', $fullName)
        }

        if ($description) {
            $params.Add('Description', $description)
        }

        if ($email) {
            $params.Add('EmailAddress', $email)
        }

        if ($office) {
            $params.Add('Office', $office)
        }

        if ($title) {
            if ($user.Title) {
                $params.Add('Title', $title)
            } else {
                $params.Add('Title', $title)
            }
        }

        if ($phone) {
            if ($user.OfficePhone) {
                $params.Add('OfficePhone', $user.OfficePhone + ', ' + $phone)
            }
            else {
                $params.Add('OfficePhone', $phone)
            }
        }

        if ($enableCheckbox.Checked) {
            $params.Add('Enabled', $true)
        }

        if ($disableCheckbox.Checked) {
            $params.Add('Enabled', $false)
        }

        Set-ADUser @params
        [System.Windows.Forms.MessageBox]::Show("User info updated successfully.")
    }
    else {
        [System.Windows.Forms.MessageBox]::Show("User not found.")
    }
})



$form.Controls.Add($updateButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(220, 280)
$cancelButton.Size = New-Object System.Drawing.Size(100, 30)
$cancelButton.Text = "Cancel"
$cancelButton.Add_Click({
    $form.Close()
})

$form.Controls.Add($cancelButton)


# Create a new button to reset the form
$resetButton = New-Object System.Windows.Forms.Button
$resetButton.Location = New-Object System.Drawing.Point(340, 280)
$resetButton.Size = New-Object System.Drawing.Size(100, 30)
$resetButton.Text = "Reset"
$resetButton.Add_Click({
    # Check if the user name is empty
    if ($usernameTextbox.Text -eq "") {
        [System.Windows.Forms.MessageBox]::Show("Please enter a user name before resetting the form.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
    }
    else {
        # Clear the form fields
        $usernameTextbox.Text = ""
        $fullNameTextbox.Text = ""
        $descriptionTextbox.Text = ""
        $emailTextbox.Text = ""
        $officeTextbox.Text = ""
        $titleTextbox.Text = ""
        $phoneTextbox.Text = ""
        $sipTextbox.Text = ""
        $enableCheckbox.Checked = $false
        $disableCheckbox.Checked = $false
        $updateButton.Enabled = $false
        
    }
})





$form.Controls.Add($resetButton)

# Display the form
$form.ShowDialog() | Out-Null
$form.Text = "Edit User Info"
$form.Width = 1000
$form.Height = 500

Just change these lines on the main form to change form size and text.

Theres actually 2 forms with the same name “$form” as well as 2 $label1 objects.

Looks like the script was copied twice.

1 Like

Thank you i didnt saw this