Change computer name on Windows devices

In this blog post, we’ll walk through the steps of changing a computer name using remediation scripts on Windows devices.

But why?

There are many reasons for wanting to rename devices across your organization, including:

  • Enforcing a naming convention makes device identification easier.
  • A consistent naming structure improves reporting and management efficiency.
  • Naming based on user roles, device types, or locations streamlines IT operations.
  • Using hostnames in device filters.
  • Dynamic group queries.

By using Intune remediation scripts, you can:

  • Automate the process of renaming devices.
  • Ensure devices adhere to compliance and configuration policies.
  • Standardize naming conventions across the environment.

Define the Naming Convention

Before creating a remediation script, you need to determine a standardized naming convention for your devices. For example:

  • Prefix based on location (e.g., NY for New York, SE for Sweden).
  • Suffix based on the department (e.g., Sales, IT).
  • Numeric identifier for uniqueness (e.g., 001, 002) or serial number.

An example format could be: NY-SALES-001, where:

  • NY = location,
  • SALES = department,
  • 001 = unique identifier.

Detection script

First we create a PowerShell script to check the current device’s name. In this case I want a country code and the serial number as hostname:

PowerShell
# Define the prefix
$prefixCountry = "FR"

# Another example
# $prefixCountryDep = "FR-Sales-"

# Generates a random 3-digit number for uniqueness
# $Identifier = (Get-Random -Minimum 100 -Maximum 999)

# Get the serial number
$serialNumber = (Get-WmiObject -Class Win32_BIOS).SerialNumber

# Define the new computer name
$newComputerName = "$prefixCountry-$serialNumber"

# Another example
#$newComputerName = "$prefixCountryDep-$Identifier"

# Get the current computer name
$currentComputerName = (Get-WmiObject -Class Win32_ComputerSystem).Name

# Check if the current computer name is the same as the new computer name
if ($currentComputerName -eq $newComputerName) {
    # The computer name is already correct, exit with a zero status code
    Write-Output "The computer name is already correct: $currentComputerName"
    exit 0
} else {
    # The computer name is not correct, exit with a non-zero status code
    Write-Output "The computer name is not correct. Current: $currentComputerName, Expected: $newComputerName"
    exit 1
}

This script:

  1. Defines variables for location, department, and a random identifier.
  2. Constructs a new computer name using these variables.
  3. Checks if the current computer name matches the new one.
  4. If it doesn’t match, trigger the remediation script.

Remediation script

We use PowerShell script to change the device’s name. Again I want the country code and serial number to be set:

PowerShell
# Define the prefix
$prefixCountry = "FR"

# Another example
# $prefixCountryDep = "FR-Sales-"

# Generates a random 3-digit number for uniqueness
# $Identifier = (Get-Random -Minimum 100 -Maximum 999)

# Get the serial number
$serialNumber = (Get-WmiObject -Class Win32_BIOS).SerialNumber

# Define the new computer name
$newComputerName = "$prefixCountry-$serialNumber"

# Another example
#$newComputerName = "$prefixCountryDep-$Identifier"

# Try to change the computer name
try {
    Rename-Computer -NewName $newComputerName -ErrorAction Stop
    # The computer name was changed successfully, exit with a zero status code
    Write-Output "Computer name changed successfully to $newComputerName"
    exit 0
} catch {
    # Failed to change the computer name, exit with a non-zero status code
    Write-Output "Failed to change computer name to $newComputerName"
    exit 1
}

This script:

  1. Defines variables for location, department, and a random identifier.
  2. Constructs a new computer name using these variables.
  3. Checks if the current computer name matches the new one.
  4. If it doesn’t match, it renames the computer.
  5. The name will change on the next restart of the computer. That can be added to the script but be careful with restarts .

Setup a Remediation in Intune

Now that you have the PowerShell scripts, it’s time to deploy it.

  1. Sign in to Intune admin portal
  2. Navigate to Devices > Windows and Scripts and remediations.
  3. Click + Create to create a new remediation
  4. Name the script something meaningful, such as “Rename-Computer SE.”
  5. Upload the scripts

Assign the Script to Devices.

Assign the script to the device group(s) where you want to apply the computer name change. This could be based on location, department, or other attributes in Entra ID.
I usually assign it to a dynamic group based out of something nice or using the DUDE automation. More info abut DUDE can be found here and the DUDE script can be found here.

Any luck?

After deploying the remediation script, you can monitor the results through the Monitor tab of the script. This will allow you to:

  • View compliance reports.
  • Identify any devices that haven’t successfully changed their name.
  • Troubleshoot any issues with the renaming process.

Using Intune remediation scripts to rename computers offers an efficient and scalable solution. By automating the process, you ensure that devices are consistently named according to your organization’s standards, which improves reporting and management. Leveraging Intune’s scripting capabilities along with proactive remediations ensures that your devices stay compliant with the naming conventions you’ve set.

Happy renaming!

Leave a Reply

Your email address will not be published. Required fields are marked *