Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, February 6, 2014 2:57 PM
I'm starting to learn Powershell and I'm stuck trying to automate a computer name change. The code below works to rename the computer and reboot but I would like to shorten it and I'm not sure how. The serial number is 15 characters which makes the computer name with the prefix added 18 charactes long. How can I modify it so that it takes the last 10 of the serial number plus the prefix? We are using Powershell 4.
The script changes the name to something like MCT1234567890ABCDE
What I would like is MCT67890ABCDE
$serial = Get-WmiObject win32_bios | select -expand serialnumber
$newname = "MCT" + $serial
Rename-Computer -NewName $newname -Force -Restart
All replies (6)
Thursday, February 6, 2014 3:42 PM âś…Answered
Try this change
$newname = "MCT" + $serial.SubString(5, 10)
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.
Thursday, February 6, 2014 3:49 PM
In my example you do not need to add 'MCT' to the beginning of the new computer name. This is because it is removing the substring (12345) and replacing it with nothing, thus pushing MCT and 67890ABCDE together.
$Serial = 'MCT1234567890ABCDE'
$Sub = $Serial.Substring(3,5)
$Serial = $Serial.Replace($Sub,'')
Thursday, February 6, 2014 4:13 PM
Tommy,
MTC isn't being returned by his command to get the serial number, he is prefixing MTC to the serial number, so it would just be easier to do the SubString on the serial number and grab the last 10 characters, then prefix MTC.
From your example I take it as the serial number comes back as 'MCT1234567890ABCDE' which is not the case.
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.
Thursday, February 6, 2014 4:17 PM
Hi,
The serial number is 15 characters which makes the computer name with the prefix added 18 charactes long.
You might run into problems with this, considering there's a 15 character limit on NetBIOS names:
http://support.microsoft.com/kb/909264/en-us
Just something to be aware of.
Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)
Thursday, February 6, 2014 4:24 PM
Tommy,
MTC isn't being returned by his command to get the serial number, he is prefixing MTC to the serial number, so it would just be easier to do the SubString on the serial number and grab the last 10 characters, then prefix MTC.
From your example I take it as the serial number comes back as 'MCT1234567890ABCDE' which is not the case.
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.
It looks like you are correct. In that case he should simply pull the substring as you've indicated. If anything, I wrapped my head around being able to extract and then replace a substring that is neither at the front or end of the string. Cheers.
Thursday, February 6, 2014 6:17 PM
Try this change
$newname = "MCT" + $serial.SubString(5, 10)If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.
This seems to work and is nice and short. Thanks
Thanks to everyone else that responded. I will keep the code in mind for future reference.