5

I'm writing a PowerShell script using Rename-Computer, and I'm hitting an odd issue:

PS C:\Users\Administrator> Rename-Computer -NewName 395_s2

Rename-Computer : Skip computer 'ztest' with new name '395_s2' because the new name is not valid. The new computer name entered is not properly formatted. Standard names may contain letters (a-z, A-Z), numbers (0-9), and hyphens (-), but no spaces or periods (.). The name may not consist entirely of digits, and may not be longer than 63 characters.

I would really prefer to be able to use underscores in the machine names, and the -Force flag doesn't change the output.

When I enter the same name manually in Computer->System Properties->Computer Name/Domain Changes, it asks:

Do you want to use this computer name?

The name "395_s2" contains non-standard characters. This might cause problems with some applications or network hardware.

I have the option to choose yes and override the non-standard characters. Is there a way to do this directly in PowerShell?

Community
  • 1
  • 1
Fsaresh
  • 61
  • 2
  • 4

2 Answers2

4

Using WMI doesnt seem to have that limitation. I just tested it on my own computer

(Get-WmiObject Win32_ComputerSystem).Rename("te_st")

It's not Rename-Computer but it looks like it works. Also going to repost my comment on Mad's post. There is a really good post about computer names and underscores. The use of the underscore in host names

To quote one of the answers as well as agreeing with Mad Scientist

Though its valid to use underscores it not a recommended practice.

Community
  • 1
  • 1
Matt
  • 45,022
  • 8
  • 78
  • 119
  • Thank you very much! I needed to add some credentials in there as well for some reason, but that worked like a charm. – Fsaresh Jul 18 '14 at 01:06
0

As an answer I am not going to show you how to get it to rename your computer with underscores, instead I will strongly suggest that you do not use them in your computer names.

I would like to refer you to RFC 1178, which is designed solely with the purpose of helping people make good decisions when naming their computers. You can find it at:

https://www.rfc-editor.org/rfc/rfc1178

As to why you shouldn't, there are a number of applications and systems that will not work if your hostname has an underscore in it, including things as standard as Internet Explorer.

Plus systems such as DomainKeys and service records use the underscore as a means to assure that their special character is not confused with hostnames (ref: Wikipedia).

Community
  • 1
  • 1
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • 1
    Unfortunately, I'm not the one who chose to name these computers with this naming scheme. My only option is to continue with the current naming scheme involving underscores. These are mostly testing machines, and they probably won't even be running things as standard as Internet Explorer. – Fsaresh Jul 16 '14 at 22:55
  • Are they going to be connected to anything else? DNS records are going to have issues with them if there is an underscore in the name as it reserves that character for service records. Maybe you could suggest to the powers that be to use a hyphen instead since A-Z, 0-9, and the hyphen (-) are the only valid characters for a computer name technically speaking. – TheMadTechnician Jul 16 '14 at 23:19
  • I'll bring it up with the people in charge of these machines, but you'd figure there would be a way to override it in PowerShell since there's a way to override it manually. If you can think of/find anything else, please let me know! – Fsaresh Jul 17 '14 at 00:30
  • 1
    Agreed that conflicts can arise. However in many modern environments such conflicts might not arise. I build a dev environment for my test Domain with sharepoint using underscores with a working dns environment and have not had issues as of yet. Also for good reading: http://stackoverflow.com/questions/10959757/the-use-of-the-underscore-in-host-names – Matt Jul 17 '14 at 01:42