Send variable to Rename-Computer

Hello, I am new at Powershell. I would like to know if there is way to pass a value from a variable to rename a computer in Powershell. I am using Rename-Computer -Newname “ComputerName” statement. I know that if I put the computer name in quotation marks it works, however I would like to use a text box and use the text as the variable for the computer name. For example the variable of the textbox may be $textbox. The Powershell statement use would be Rename-Computer -Newname $textbox in script file. This does not work. How do I pass the value pf $textbox to the -Newname. Does anybody have any idea of how to work around this issue?

Hi, welcome to the forum :wave:

Firstly, when posting code in the forum, please can you use the preformatted text </> button. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working).

As to your question, how are you generating your text box? Is it on a form, or just a popup?

If it’s a form, using the TextBox class, then you use the Text property e.g. $textBox.Text

First thank you for replying. Here is part of the code, please be aware that is it only part of the code to explain where I get the data from. The textbox is generated in as part a form.

Getting the department number is to be used as part of computer name

$DeptID = New-Object System.Windows.Forms.TextBox
$DeptID.Location = New-Object System.Drawing.Point(10,110)
$DeptID.Size = New-Object System.Drawing.Size(260,20)

Getting serial number to be used as part of the computer name

$SerialNumber = (Get-WmiObject -class win32_bios).SerialNumber

Concatenating department and serial number to be used as new computer name

the ‘-’ is used to separate department and serial number.

$NewComputerName = $DeptID.text + ‘-’ + $SerialNumber

Changing computer name

Rename-Computer -Newname $NewComputerName

Please aware that I can not put the computer name directly in quotes because it is to be used with different computers not just one that I why I need to use a variable.

Please edit your post and use the preformatted text button </> to format your code.

I am sorry I do not understand to why need to </> for the code. I am not using html. I using a ps1 with script code. Maybe I am in the wrong forum. This question is more for the programmers then the administrators. I just need to know how do I pass the value of variable $NewcomputerName to the object Rename-Computer. This is part of the argument for the -Newname of the Rename-Object.

Hi, not using the correct method for posting your code makes it harder to read, and harder to test because it requires editing before it will run in VSCode or the ISE.
The forum may also strip out some elements of the code. We’re not asking you to write HTML.
Simply click the </> button and paste your code where it tells you to.

You may need to explain a bit further the problem you’re having because on the face of it, your code does look OK. You don’t need to put the variable in quotes unless you want to expand it. So this:

$NewComputerName = $DeptID.text + '-' + $SerialNumber

and this:

Rename-Computer -Newname $NewComputerName

look fine.


Thanks got it, here is the code in </> Hope this helps.

$DeptID = New-Object System.Windows.Forms.TextBox
$DeptID.Location = New-Object System.Drawing.Point(10,110)
$DeptID.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($DeptID)e or paste code here

$SerialNumber = (Get-WmiObject -class win32_bios).SerialNumber
$NewComputerName = $DeptID.text + ‘-’ + $SerialNumber

Rename-Computer -Newname $NewComputerName
1 Like

Thanks. Can you elaborate on the problem you’re having? Your code looks fine and tests OK too.
Obviously I didn’t rename my computer but $NewComputerName had the expected value.

I got an error on this line :laughing:

1 Like

That is because you are missing part of the code where the form is built missing. To further make things too more complex it is call from a cmd.exe batch line. Trust me the forms works. I need something like this How to Get a Computer Name with PowerShell but I can not seem to put my finger on the syntax. Here is the code in case somebody wants to take at crack it. The first is the batch file. The second is teh script file. Use it with caution because it will change the description of the computer and the name for the computer (this part is not working) and it will open the control panel to see the changes computer description and computer name.

This is the batch file.

Echo on

 Powershell ".\GetUserInput.ps1"
 powershell.exe -command exit 
type or paste code here

Here is full scripting file code.

# **** Get Administrator rights for the user ********

Set-ExecutionPolicy Bypass -Scope CurrentUser -Force
function Test-Admin {
    $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())


if ((Test-Admin) -eq $false)  {
    if ($elevated) {
        # tried to elevate, did not work, aborting
    } else {
        Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))

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

$form = New-Object System.Windows.Forms.Form
$form.Text = '2022-Misd IT Dept'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,135)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,135)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,10)
$label.Size = New-Object System.Drawing.Size(280,20)
$CD = Get-WmiObject -Class Win32_OperatingSystem |Select Description
 $Label.text = "$cd".substring("$cd".IndexOf("{") + 1, "$cd".IndexOf("}") - 2)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,30)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please enter new computer description:'

$CompDesc = New-Object System.Windows.Forms.TextBox
$CompDesc.Location = New-Object System.Drawing.Point(10,50)
$CompDesc.Size = New-Object System.Drawing.Size(260,20)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,70)
$label.Size = New-Object System.Drawing.Size(280,20)
$CN = $env:computername | Select-Object
$label.Text = 'Computer Name=' + $CN

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,90)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please deparment ID:'

$DeptID = New-Object System.Windows.Forms.TextBox
$DeptID.Location = New-Object System.Drawing.Point(10,110)
$DeptID.Size = New-Object System.Drawing.Size(260,20)

$form.Topmost = $true

$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
     # // Chane computer Description 

     $ComuterDescription = Get-WMIObject Win32_OperatingSystem
     $ComuterDescription.Description =  $CompDesc.Text   
     $SerialNumber = (Get-WmiObject -class win32_bios).SerialNumber 

      $NewComputerName = $deptid.text + '-' + $SerialNumber
     Rename-Computer -NewName $NewComputerName
    stop-process -Id $PID

 $form = New-Object System.Windows.Forms.Form
 stop-process -Id $PID
type or paste code here

Lol no I’m sorry I didn’t really run it. I was making a joke because the default text for the preformatted text option was mostly still present on the end of this line of code. :innocent:

type or paste code here

When you click the </> button that text is automatically highlighted and you can paste immediately to overwrite it. Somehow it didn’t get erased or overwrote when you pasted your code.

That looks OK to me. I made a couple of minor changes to output the variables to a file instead of actually changing my computer details but it worked as expected.

I note you’re launching sysdm.cpl immediately after the change. I have tested on a VM and the change did show straightaway in sysdm.cpl for me, although hostname still showed the old name. Have you tried rebooting to see if it’s changing the name but just not updating in sysdm.cpl?

The problem is with hostname. If you the host name directly it will changed instead of variable it will do and it show without rebooting. What I was make a copy of the actual host name and used the direct name and it changes. Since I did not force the change (the script) I am still change it back to the original name. To answer your question if I used a variable even I reboot it will not change that name. That is issue that have. I need to use a variable because not all machines will have the same name. I think the answer may lie in using the Invoke-Command to pass the parameters (the $NewComputerName variable) to to the Rename-Compute , but I have not figure it out. The other option I was thinking last night was to create a variable in environment and set it to $NewComputerName (Set /p NewComputer= $NewComputerName) and then call the variable from the environment using Rename-Computer -Newname %NewComputer, but I not sure if this will work Powershell since the syntax is for Cmd.exe as far setting the variables to the environment.

Sorry, I’m not sure I can be much more help because I don’t understand what the problem is.

I’ve run your code on a test machine, using your batch file and your script and it works as expected. The description is updated and the machine name is changed.

Does it change the name of machine with out rebooting (Do you see the change on Sysdm.cpl) and are you using a variable to change the name of the machine?

Yes, I see the change on Sysdm.cpl.

I am using your code exactly as copied and pasted above.

Wow this is very puzzling. The only difference that I see is that you are connecting to a domain, while mine connects to a workgroup. I am not sure if that has affect on the outcome. Mine will be used on a domain, but we are at holiday break so I am doing this pet project at home and I using a workgroup.

I think I am getting close to what I need. By using the following code I am able to
rename the local computer. Now it is a matter of find out to passing the $NewComputerName to the host if that is possible.

$pcname = Read-Host -Prompt 'Input the new PC name'
   Rename-Computer -NewName $pcname

Rename-Computer with the -NewName parameter renames the local computer. If you want to rename a remote computer you need to specifiy the computer name. If they’re workgroup computers you’ll need to provide credentials as well.

Rename-Computer -ComputerName OldComputerName -NewName $NewComputerName

Will rename the computer called OldComputerName to the value stored in $NewComputerName. You can use NETBIOS name, the FQDN, or the IP address as input to the -ComputerName parameter.

1 Like

If the syntax is correct I am still puzzled as to why it will not work with the a variable.