Link to home
Create AccountLog in
Powershell

Powershell

--

Questions

--

Followers

Top Experts

Avatar of Edward Crist
Edward Crist🇺🇸

Powershell - rename computer using serial number and add to domain
I'm using the following script to both

Rename the computer using the serialnumber in BIOS + "-21"  (example: P00035T-21)

AND

join our domain

It's joining the domain, but not changing the name

(scripting noobie here)

thanks

$serial = Get-WmiObject win32_bios | select -expand serialnumber
$newname = $serial + "-21"
Rename-Computer -NewName $newname -Force
Add-Computer -DomainName "cityhigh.lan" -OUPath "OU=2021;OU=Laptops;DC=cityhigh;DC=lan" -Restart

Open in new window

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Chris DentChris Dent🇬🇧

Your OUPath should be comma separated, not semi-colon separated. But that bit worked and the computer landed in the right OU? If so, odd but go with it :)

I wouldn't be surprised if you had to reboot before the changed name was relevant as far as "Add-Computer" is concerned.

This, however, assumes that Rename-computer is running without throwing red error messages everywhere. Is that a fair assumption?

Try to do it all in one go with the Add-Computer and its "NewName" argument.
And you'll need to use commas instead of semicolons for the OU path.
$serial = Get-WmiObject win32_bios | select -expand serialnumber
$newname = $serial + "-21"
Add-Computer -DomainName "cityhigh.lan" -OUPath 'OU=2021,OU=Laptops,DC=cityhigh,DC=lan' -NewName $newname -Restart 

Open in new window


Avatar of Edward CristEdward Crist🇺🇸

ASKER

Yeah, didn't get any red errors

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Edward CristEdward Crist🇺🇸

ASKER

oBda

Using your script, it tells me the new name is no different from the current name

It seems that $newname is NOT adding the "-21" to the $serial

Avatar of Chris DentChris Dent🇬🇧

Same computer as you've just tried? If so, you may find it's just waiting for a reboot to complete the rename.

If you haven't rebooted since your Rename-Computer, you need to do that now. A rename requires a reboot before it's fully active.
You can try to rename it to its former name before the reboot:
Rename-Computer -NewName OldName -Force

Open in new window


Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Edward CristEdward Crist🇺🇸

ASKER

Still no go.  Maybe you can't do both at once??

"no go" is not an error report based on which we can help you.
I can't test it at the moment, but I'm pretty certain that Microsoft didn't add the -NewName argument to Add-Computer just to deceive you.

Avatar of Edward CristEdward Crist🇺🇸

ASKER

Sorry....but a restart didn't help or change the name.  The error is still saying the new name is the same as the current name.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Chris DentChris Dent🇬🇧

Try a third name, just to see if that breaks the pattern a bit.

Well - is it?
And be aware that a Windows computer name is limited to 15 characters; if the serial + "21" is longer, the command might just cut off the rest.
$newname = "$(Get-WmiObject win32_bios | select -expand serialnumber)-21"
"This computer is currently named: '$(& hostname.exe)'"
"The new name based on the serial: '$($newname)'"

Open in new window


Avatar of Edward CristEdward Crist🇺🇸

ASKER

Well, oBdA, your script to show current and new name worked great...it showed the current and correct new name

However, running that along with Add-Computer, didn't work....it added to the domain, but did not change the name

And no red errors either.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Edward CristEdward Crist🇺🇸

ASKER

This is what I just ran
$newname = "$(Get-WmiObject win32_bios | select -expand serialnumber)-21"
Rename-Computer -NewName $newname -Force
Add-Computer -DomainName "cityhigh.lan" -OUPath "OU=2021,OU=Laptops,DC=cityhigh,DC=lan" -Restart

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of oBdAoBdA

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of Edward CristEdward Crist🇺🇸

ASKER

Hat's off to you....that did it!  Thanks so much
Powershell

Powershell

--

Questions

--

Followers

Top Experts

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.