top of page
Search

(182) Microsoft Intune - Set Hostname to periodically go up to a new number

  • Writer: Mr B SOE way
    Mr B SOE way
  • 1 day ago
  • 2 min read

As I prepping this yesterday for a customer, which does work https://soeintunedevice.wixsite.com/home/post/181-microsoft-intune-prompt-for-hostname-change if you want to have a read.


Install.ps1 is as follows, to explain what it does is the "Prefix" is generally what you see in

ree




Since there is no way to do this via Intune, the only way would be via PowerShell. It will start off with applying WIN11-3D-001 as the hostname



$Prefix = 'WIN11-3D-'
$regPath = "HKLM:\SOFTWARE\MrBSOEWay\RenameComputer"
$regValue = "Version"

# Check if rename already occurred
if (Test-Path $regPath) {
    Write-Output "Device already renamed. Skipping rename."
    exit
}

# Get list of existing devices from Intune registry (simulated local tracking)
$serialBase = 01
$serialFile = "$env:ProgramData\\MrBSOEWay\\serial.txt"
if (!(Test-Path $serialFile)) {
    New-Item -ItemType File -Path $serialFile -Force | Out-Null
    Set-Content -Path $serialFile -Value $serialBase
}

# Read and increment serial
$Serial = Get-Content -Path $serialFile
$NewSerial = [int]$Serial + 01
Set-Content -Path $serialFile -Value $NewSerial

# Construct new hostname
$NewComputername = $Prefix + $NewSerial.ToString("D3")
Write-Output "Renaming device to $NewComputername"
Rename-Computer -NewName $NewComputername -Force

# Create registry key for detection
New-Item -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\MrBSOEWay" -ErrorAction SilentlyContinue
New-Item -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\MrBSOEWay\RenameComputer" -ErrorAction SilentlyContinue
New-ItemProperty -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\MrBSOEWay\RenameComputer" -Name Version -Value "1.0" -PropertyType String -Force -ErrorAction SilentlyContinue

Restart-Computer

$Path = "HKLM:\Software\MrBSOEWay\RenameComputer"
$Name = "Version"
$Type = "STRING"
$Value = "1.0"
 
Try {
    $Registry = Get-ItemProperty -Path $Path -Name $Name -ErrorAction Stop | Select-Object -ExpandProperty $Name
    If ($Registry -eq $Value){
        Write-Output "Detected"
       Exit 0
    } 

    Exit 1
} 

Catch {

    Exit 1
}


Current hostname is WIN11-3D-01

ree









As I have executed the script in PowerShell ISE as administrator, I see this in the logs:

ree








After a restart, on logon by typing in . I can see my hostname has changed to WIN11-3D-002

ree


















And on Intune, the hostname has updated as well.

ree









If I try to re-run the script, I get this on the logs as device has been renamed.

ree

 
 
 

Comments


bottom of page