none
Change machine names from lowercase to uppercase RRS feed

  • Question

  • This article shows where the change in the registry needs to be made

    http://peteinman.wordpress.com/2009/01/06/changing-the-case-of-windows-computer-name/

    The values that need to be modified are "Hostname" and "NV Hostname"  Currently a machine on our network reads like the following: "machinename-123"  I want to be able to change it to "MACHINENAME-123"  Every PC on our network has the same first part of the PC name, in this example that is "machinename-"  The only thing that changes from machine to machine is the 3 digit number after the dash.

    Is there a way in Powershell or some other method to run that on all machines on our network to change those registry values to make the beginning part of the name uppercase?

    Thank you.

    Thursday, July 31, 2014 9:09 PM

Answers

  • Might not be your best option, but you can try the below script. Perhaps deploy it as a Group Policy login script...

    $ComputerName = $env:COMPUTERNAME.ToLower()
    
    $Path = 'HKLM:\System\CurrentControlSet\Services\TCPIP\Parameters'
    
    $Property = 'Hostname'
    
    Set-Location $Path
    
    Set-ItemProperty -path $Path -name $Property -value $ComputerName

    Few tweaks here. First, use .ToUpper() instead, as the OP is looking to capitalize all names. Second, you can't use a logon script, as that will run in the user context and users can't make changes to HKLM. Lastly, you don't need to use Set-Location, since you're specifying the path in Set-ItemProperty anyway. [Offtopic edit - I just looked at your profile and wanted to say thank you for your service. Much respect and appreciation.]

    $computerName = $env:COMPUTERNAME.ToUpper()
    $keyPath = 'HKLM:\System\CurrentControlSet\Services\TCPIP\Parameters'
    $keyOne = 'Hostname'
    $keyTwo = 'NV Hostname'
    
    Set-ItemProperty -Path $keyPath -Name $keyOne -Value $computerName
    Set-ItemProperty -Path $keyPath -Name $keyTwo -Value $computerName

    Still though, this code must be run locally on each machine. This would be fine if you have something like ConfigMgr to push the script and run it locally or you could try deploying this as a startup script instead. Alternatively, this can be used to remotely update these keys:

    Get-ADComputer -Filter * -SearchBase 'OU=Test PCs,DC=domain,DC=com' | ForEach-Object {
    
        $computerName = $_.Name.ToUpper()
    
        $props = @{
            ComputerName = $computerName
            Online = $false
            ChangedHostname = $false
            ChangedNVHostname = $false
        }
    
        If (Test-Connection -ComputerName $computerName -Count 1 -Quiet) {
    
            $props.Online = $true
    
            try {
    
                $hive = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $computerName)
                $keyPath = $hive.OpenSubKey('System\CurrentControlSet\Services\TCPIP\Parameters',$true)
    
                If ($keyPath.GetValue('Hostname') -cne $computerName) {
    
                    $keyPath.SetValue('Hostname',$computerName)
                    $props.ChangedHostname = $true
    
                }
    
                If ($keyPath.GetValue('NV Hostname') -cne $computerName) {
    
                    $keyPath.SetValue('NV Hostname',$computerName)
                    $props.ChangedNVHostname = $true
    
                }
    
            } catch {
    
                $props.ChangedHostname = 'ERROR'
                $props.ChangedNVHostname = 'ERROR'
    
            }
    
        }
    
        New-Object PsObject -Property $props
    
    } | Sort ComputerName | 
            Select ComputerName,Online,ChangedHostname,ChangedNVHostName | 
                Export-Csv .\updatedComputers.csv -NoTypeInformation
    
    
    Example output:
    ComputerName Online ChangedHostname ChangedNVHostname
    ------------ ------ --------------- -----------------
    JESTER        False           False             False
    W510           True           False             False
    WIN8           True            True              True



    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Friday, August 1, 2014 2:12 AM
    Moderator

All replies

  • Might not be your best option, but you can try the below script. Perhaps deploy it as a Group Policy login script...

    $ComputerName = $env:COMPUTERNAME.ToLower()
    
    $Path = 'HKLM:\System\CurrentControlSet\Services\TCPIP\Parameters'
    
    $Property = 'Hostname'
    
    Set-Location $Path
    
    Set-ItemProperty -path $Path -name $Property -value $ComputerName

    

    • Edited by SJohnsonSF Friday, August 1, 2014 12:19 AM
    Friday, August 1, 2014 12:18 AM
  • Might not be your best option, but you can try the below script. Perhaps deploy it as a Group Policy login script...

    $ComputerName = $env:COMPUTERNAME.ToLower()
    
    $Path = 'HKLM:\System\CurrentControlSet\Services\TCPIP\Parameters'
    
    $Property = 'Hostname'
    
    Set-Location $Path
    
    Set-ItemProperty -path $Path -name $Property -value $ComputerName

    Few tweaks here. First, use .ToUpper() instead, as the OP is looking to capitalize all names. Second, you can't use a logon script, as that will run in the user context and users can't make changes to HKLM. Lastly, you don't need to use Set-Location, since you're specifying the path in Set-ItemProperty anyway. [Offtopic edit - I just looked at your profile and wanted to say thank you for your service. Much respect and appreciation.]

    $computerName = $env:COMPUTERNAME.ToUpper()
    $keyPath = 'HKLM:\System\CurrentControlSet\Services\TCPIP\Parameters'
    $keyOne = 'Hostname'
    $keyTwo = 'NV Hostname'
    
    Set-ItemProperty -Path $keyPath -Name $keyOne -Value $computerName
    Set-ItemProperty -Path $keyPath -Name $keyTwo -Value $computerName

    Still though, this code must be run locally on each machine. This would be fine if you have something like ConfigMgr to push the script and run it locally or you could try deploying this as a startup script instead. Alternatively, this can be used to remotely update these keys:

    Get-ADComputer -Filter * -SearchBase 'OU=Test PCs,DC=domain,DC=com' | ForEach-Object {
    
        $computerName = $_.Name.ToUpper()
    
        $props = @{
            ComputerName = $computerName
            Online = $false
            ChangedHostname = $false
            ChangedNVHostname = $false
        }
    
        If (Test-Connection -ComputerName $computerName -Count 1 -Quiet) {
    
            $props.Online = $true
    
            try {
    
                $hive = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $computerName)
                $keyPath = $hive.OpenSubKey('System\CurrentControlSet\Services\TCPIP\Parameters',$true)
    
                If ($keyPath.GetValue('Hostname') -cne $computerName) {
    
                    $keyPath.SetValue('Hostname',$computerName)
                    $props.ChangedHostname = $true
    
                }
    
                If ($keyPath.GetValue('NV Hostname') -cne $computerName) {
    
                    $keyPath.SetValue('NV Hostname',$computerName)
                    $props.ChangedNVHostname = $true
    
                }
    
            } catch {
    
                $props.ChangedHostname = 'ERROR'
                $props.ChangedNVHostname = 'ERROR'
    
            }
    
        }
    
        New-Object PsObject -Property $props
    
    } | Sort ComputerName | 
            Select ComputerName,Online,ChangedHostname,ChangedNVHostName | 
                Export-Csv .\updatedComputers.csv -NoTypeInformation
    
    
    Example output:
    ComputerName Online ChangedHostname ChangedNVHostname
    ------------ ------ --------------- -----------------
    JESTER        False           False             False
    W510           True           False             False
    WIN8           True            True              True



    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Friday, August 1, 2014 2:12 AM
    Moderator
  • Thanks for the kind words and tips, Mike! I'm fairly new to PowerShell and am very eager to learn as much as I can!
    Friday, August 1, 2014 5:19 PM