# Setting the computer name

The PC will automatically generate a computer name, but if you'd like to set your own standard, you will need to set the variable [COMPUTERNAME ](/deployr/task-sequence-variables.md)before the Apply OS Step while in the WinPE stage or leverage the Rename-Computer function while in the Full OS. &#x20;

### Using PowerShell:

See a simple example [Set Computer Name with Script](/deployr/powershell-modules/simple-prompt-for-computer-name.md).  Basically it comes down to setting this task sequence variable to what you want the computer to be named.

```powershell
${TSEnv:ComputerName} = CUSTOMNAME #15 Char or less
```

### Using Set variable step

You can also use a Set variable to simply set the name, in this example I'm naming the computer "DC", which is typically a horrible name, but this task sequence is for creating my Lab's Domain Controller, so it actually makes sense for this.  However, using a "Set variable" step typically is a bad way to do it, as it will set the name to this each time, all the more reason to script your process to build the name you want based on serial, asset tag, etc.<br>

<figure><img src="/files/dXhBPB5F9WrM9Vcptg6t" alt=""><figcaption></figcaption></figure>

You will notice that the variable is set before the "Apply Windows... " step so that the task sequence will consume that variable and apply it to the unattended file.

### PowerShell Function Rename-Computer in Full OS

Leveraging the built in PowerShell function "Rename-Computer" once in the full OS, it is also possible to set the computer name. I do this for Hyper-V lab machines to automatically grab the Hyper-V VM name and use that for the device.  See example below.<br>

```powershell
function Get-HyperVName {
    [CmdletBinding()]
    param ()
    if ($env:SystemDrive -eq 'X:'){
        Write-host "Unable to get HyperV Name in WinPE"
    }
    else{
        if (((Get-CimInstance Win32_ComputerSystem).Model -eq "Virtual Machine") -and ((Get-CimInstance Win32_ComputerSystem).Manufacturer -eq "Microsoft Corporation")){
            $HyperVName = Get-ItemPropertyValue -Path 'HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters' -Name "VirtualMachineName" -ErrorAction SilentlyContinue
        }
        return $HyperVName
    }
}
function Set-HyperVName {
    [CmdletBinding()]
    param ()
    $HyperVName = Get-HyperVName
    Write-Output "Renaming Computer to $HyperVName"
    Rename-Computer -NewName $HyperVName -Force 
}

try {Set-HyperVName}
catch{}
```

### Offline Domain Join

If you're doing offline domain join, just make sure you've set the COMPUTERNAME variable before the Offline Domain Join step, or it will default to the standard set in that step, which is serial number.  If you have pre-set the COMPUTERNAME variable, the Offline domain join step will honor that variable and not change it to the %SERIALNUMBER% variable.  Note that if your Serial Number is logner than 15 characters, it will break the process, so recommend doing something like PC-%SERIALNUMBER:12% so it will set a prefix of PC- and then only grab 12 characters of the Serial Number.

{% hint style="info" %}
Computers that are members of an Active Directory domain can't have names that contain only numerals. This is a DNS restriction. For More information: <https://learn.microsoft.com/en-us/troubleshoot/windows-server/active-directory/naming-conventions-for-computer-domain-site-ou>
{% endhint %}

<figure><img src="/files/2TCeBwQ1A9qR0teRZ8rj" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://documentation.2pintsoftware.com/deployr/setting-the-computer-name.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
