Renaming PC with Drop Down Menus

Hi Guys,

I could not find a script anywhere on the net to create a pop box, with drop down menus, so I took a couple of pieces from different blogs, and created my own. I am new to this and would appreciate some help.

I want to join the info from the drop down menus to create a new PC name, but I can not get the output to the host. Can someone perhaps help?

Any help will be greatly appreciated.

Here is a link to my script at GistHubGist:


<script src=“”></script>


Your help is really appreciated





Its not easy to look at this code until its properly formated, you could use to post you code and update the post with you link by removing the code here.

First, I would probably try to use a hashtable or psobject rather than arrays. Something like this:

You should be able to do something like this:

$deviceType = @()
$deviceType += [pscustomobject]@{
    Type   = 'DESKTOP'
    Prefix = 'WS'
$deviceType += [pscustomobject]@{
    Type   = 'LAPTOP'
    Prefix = 'LT'

$site= @()
$site += [pscustomobject]@{
    Name   = 'BUTTERWORTH'
    Suffix = 'BUT'
$site += [pscustomobject]@{
    Name   = 'BIZANA'
    Suffix = 'BZA'

$cmbDevType.displaymember = "Type"
$cmbDevType.ValueMember = "Value"

$cmbSite.displaymember = "Type"
$cmbSite.ValueMember = "Value"

Then you just need to glue the values together:

'{0}-{1}-{2}' -f $cmbDevType.ValueMember,$cmbSite.ValueMember,(Get-Random -Minimum 10000 -Maximum 99999)

Which would give you something like LT-BZA-58392. Didn’t test this, but the link above appears to have implemented something similar

Thank you for your reply Rob


I have removed the “Arrays” and changed it to the following:


This took away the need to have the two arrays.


I will have a look at your idea and let you know



I have posted it to GitHubGist


Here is the link to the latest code:

<script src=“”></script>




As for this…

I could not find a script anywhere on the net to create a pop box, with drop down menus

… where did you look. As there is a module for this type of thing in the MS PowerSellGallery.

Find-Module -Name 'AnyBox' | Format-Table -AutoSize

Version Name   Repository Description
------- ----   ---------- -----------
0.3.3   AnyBox PSGallery  Designed to facilitate script input/output with an easily customizable WPF window

The authors doc on the tool, specifically the drop down part.

$prompt = New-AnyBoxPrompt -Name 'fav_sport' `
  -Message 'What is your favorite sport?' `
  -ValidateSet @('Basketball', 'Football', 'Baseball', 'Soccer', 'Hockey', 'Other') `
  -DefaultValue 'Baseball'

Show-AnyBox -Icon 'Question' -Prompt $prompt -Buttons 'OK'

This uses also the more modern, support WPF (Windows Presentations Foundation) vs WF (Windows Forms) that your code is using.


Secondly, rule of thumb, when you get into a space of If/then exceeds say 3 or more, it’s time to look at using a switch statement, for ease of readability, maintenance, etc. You also almost never read Write-Host, unless you are using foreground or background colors or other custom formatting needs. If you are looking to just send to the screen, Write-Output is the default, so, you don’t need Write-* at all.


function Return-DropDown 
    $script:Choice1 = $DropDownType.SelectedItem.ToString()
    switch ($Choice1.Text) 
        'DESKTOP' { $Desktop | Out-Default}
        'LAPTOP'  { $LAPTOP | Out-Default }
        default   { 'The choice could not be determined.' }

function Return-DropDown 
    $script:Choice2 = $DropDownRegion.SelectedItem.ToString()

    switch ($Choice2.Text) 
        'ZWELITSHA'         { $Site00 | Out-Default }
        'BUTTERWORTH'       { $Site02 | Out-Default }
        'BIZANA'            { $Site03 | Out-Default }
        'COFIMVABA'         { $Site04 | Out-Default }
        'CRADOCK'           { $Site05 | Out-Default }
        'EAST LONDON'       { $Site06 | Out-Default }
        'FORT BEAUFORT'     { $Site07 | Out-Default }
        'GRAHAMS TOWN'      { $Site08 | Out-Default }
        'GRAAF REINET'      { $Site09 | Out-Default }
        'IDUTYWA'           { $Site10 | Out-Default }
        'JJ SERFONTEIN'     { $Site11 | Out-Default }
        'KING WILLIAMSTOWN' { $Site12 | Out-Default }
        'LIBODE'            { $Site13 | Out-Default }
        'LUSIKISIKI'        { $Site14 | Out-Default }
        'MT AYLIFF'         { $Site18 | Out-Default }
        'MT FLETCHER'       { $Site15 | Out-Default }
        'MT FRERE'          { $Site16 | Out-Default }
        'MALUTHI'           { $Site17 | Out-Default }
        'MTHATHA'           { $Site19 | Out-Default }
        'NGCOBO'            { $Site20 | Out-Default }
        'PORT ELIZABETH'    { $Site21 | Out-Default }
        'QUMBU'             { $Site22 | Out-Default }
        'QUEENSTOWN'        { $Site23 | Out-Default }
        'STERKSPRUIT'       { $Site24 | Out-Default }
        'UITENHAGE'         { $Site25 | Out-Default }
        'LADY FRERE'        { $Site28 | Out-Default }
        default             { 'The choice could not be determined.' }

And as a rule, unless you need to expand variables, use single quotes for strings. If you only have one variable, that you are using / outputting, it does not really needs double quotes either. It does not hurt having them there, but just saying.

As for…

I am new to this and would appreciate some help.

You can start with clicking and using the resources on this site, by clicking the ‘Free Resources’ link in the left navigation pane as well as these resource threads.

And this…

Hi Rob,


Do I leave the functions as they are, or do I delete them? I am totally new to this. I understand the logic in what you have given me, but not sue how to fit it into my script.




Thank you for your help!

This is my first time trying powershell, and I have tried to use some stuff that I got to try and make mine work.

I will definitely look into this and add it. Will let you know what the outcome is.




Take a look at this. I remarked out some things that were unnecessary. Being a beginner, doing a GUI first is difficult and as you learn more the probability of leveraging a function over a GUI. It’s going to much easier to learn be stripping away all of the GUI components and learning Powershell.

A label was added so when you press OK, the concatenated string will be shown.

$deviceType = @()
$deviceType += [pscustomobject]@{
    Type   = 'DESKTOP'
    Prefix = 'WS'
$deviceType += [pscustomobject]@{
    Type   = 'LAPTOP'
    Prefix = 'LT'

$site= @()
$site += [pscustomobject]@{
    Name   = 'BUTTERWORTH'
    Suffix = 'BUT'
$site += [pscustomobject]@{
    Name   = 'BIZANA'
    Suffix = 'BZA'

#$a = New-Object -comobject
#$b = $a.popup(“This will rename, and add your PC to the new ECDOE Domain “,0,“Message from BlackOps XXX”,1)

# Form building

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$Form=New-Object System.Windows.Forms.Form
$Form.width = 550
$Form.height = 300
$Form.Text = ”Rename Device”
#$Form.BackColor = [System.Drawing.Color]:: "green"
$Form.StartPosition = "CenterScreen"

$DropDownType = New-Object System.Windows.Forms.ComboBox
$DropDownType.DropDownStyle = 'DropDownList'
$DropDownType.AutoCompleteSource = 'ListItems'
$DropDownType.AutoCompleteMode = 'Append'
$DropDownType.DisplayMember = "Type"
$DropDownType.ValueMember = "Value"
$DropDownType.Location = New-Object System.Drawing.Size(200,10)
$DropDownType.Size = New-Object System.Drawing.Size(150,30)
$DropDownType.Font = 'Microsoft Sans Serif,12'
$DropDownType.SelectedIndex = 0


$DropDownTypeLabel = New-Object System.Windows.Forms.Label
$DropDownTypeLabel.Location = New-Object System.Drawing.Size(10,10)
$DropDownTypeLabel.Size = New-Object System.Drawing.Size(160,30)
$DropDownTypeLabel.Font = 'Microsoft Sans Serif,8'
$DropDownTypeLabel.Text = "TYPE:"

# Function to return the name of the selected "type" from the first DropDown box
#function Return-DropDownType {
#    $Choice1 = $DropDownType.SelectedItem.ToString()
#   ForEach ($Item in $MyDropDownType) {
#       $DropDownRegion.Items.Add($Item)
#   } 

$DropDownRegion = New-Object System.Windows.Forms.ComboBox
$DropDownRegion.DropDownStyle = 'DropDownList'
$DropDownRegion.AutoCompleteSource = 'ListItems'
$DropDownRegion.AutoCompleteMode = 'Append'
$DropDownRegion.DisplayMember = "Name"
$DropDownRegion.ValueMember = "Suffix"
$DropDownRegion.Location = New-Object System.Drawing.Size(200,55)
$DropDownRegion.Size = New-Object System.Drawing.Size(210,30)
$DropDownRegion.Font = 'Microsoft Sans Serif,12'
$DropDownRegion.SelectedIndex = 0


$DropDownRegionLabel = New-Object System.Windows.Forms.Label
$DropDownRegionLabel.Location = New-Object System.Drawing.Size(10,55)
$DropDownRegionLabel.size = New-Object System.Drawing.Size(160,30)
$DropDownRegionLabel.Text = "REGION:"

# Function to return the name of the selected "region" from the second DropDown box
#function Return-DropDownRegion {
#    $Choice2 = $DropDownRegion.SelectedItem.ToString()


 #ForEach ($Item in $MyDropDownRegion) {
 #   $DropDownRegion.Items.Add($Item)
 #   } 


## Input-Box for Persal number

$TextBoxPersalNumber = New-Object System.Windows.Forms.TextBox
$TextBoxPersalNumber.Location = New-Object System.Drawing.Point(200,105) 
$TextBoxPersalNumber.Size = New-Object System.Drawing.Size(80,30)
$TextBoxPersalNumber.Font = 'Microsoft Sans Serif,13' 


$TextBoxPersalNumberLabel = New-Object System.Windows.Forms.Label
$TextBoxPersalNumberLabel.Location = New-Object System.Drawing.Size(10,105)
$TextBoxPersalNumberLabel.size = New-Object System.Drawing.Size(160,30)
$TextBoxPersalNumberLabel.Text = "PERSAL NUMBER:"


## Temp label ###
$TempLabel = New-Object System.Windows.Forms.Label
$TempLabel.Location = New-Object System.Drawing.Size(10,150)
$TempLabel.size = New-Object System.Drawing.Size(400,30)
$TempLabel.Text = "Test String"

## OK Button ###

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(290,190)
$OKButton.Size = New-Object System.Drawing.Size(120,35)
$OKButton.Font = 'Microsoft Sans Serif,10'
$OKButton.ForeColor = [System.Drawing.Color]:: "white"
$OKButton.BackColor = [System.Drawing.Color]:: "green"
$OKButton.Text = "OK"

    if ($TextBoxPersalNumber.Text.Length -lt 1) {
        $msg = 'Number cannot be empty!!'
    else {
        $msg = '{0}-{1}-{2}' -f $DropDownType.SelectedItem.Prefix,$DropDownRegion.SelectedItem.Suffix,$TextBoxPersalNumber.Text

    $TempLabel.Text = $msg
    #$Form.DialogResult = "OK"


## Cancel Button ##

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(400,190)
$CancelButton.Size = New-Object System.Drawing.Size(120,35)
$CancelButton.Font = 'Microsoft Sans Serif,10'
$CancelButton.ForeColor =  "white"
$CancelButton.BackColor =  "red"
$CancelButton.Text = "Cancel"

    $Form.DialogResult = "Cancel"



$result = $Form.ShowDialog()
Damn Rob!
This is just awesome! Thank you so much!
I added all the other sites, and added the following at the end: "write-host $NewName.Text" This allows the next part of my script to rename the pc without restarting it.
One more question if I may? How do I restrict the text input to eight characters only?