Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Rename-Computer issue -- names with underscores

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?

like image 731
Fsaresh Avatar asked Sep 30 '22 07:09

Fsaresh


1 Answers

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.

like image 134
Matt Avatar answered Nov 15 '22 04:11

Matt