(181) Microsoft Intune - Prompt for Hostname Change
- Mr B SOE way
- 1 day ago
- 2 min read
Updated: 6 hours ago
A customer who wanted a way for end users to get prompted to change hostname for their devices, and I remember I did something like this a few years ago (simple but easy to do).
You need to prep for the following:
ServiceUI.exe (which you can grab easily from any MDT installer)
RenameComputer.ps1 - is a form that will appear to prompt the end user to change their hostname.
Add-Type -AssemblyName System.Windows.Forms
# Create the form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Change Hostname"
$form.Size = New-Object System.Drawing.Size(300,150)
$form.StartPosition = "CenterScreen"
# Label
$label = New-Object System.Windows.Forms.Label
$label.Text = "Enter new hostname:"
$label.AutoSize = $true
$label.Location = New-Object System.Drawing.Point(10,20)
$form.Controls.Add($label)
# Textbox
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,50)
$textBox.Width = 260
$form.Controls.Add($textBox)
# OK button
$okButton = New-Object System.Windows.Forms.Button
$okButton.Text = "OK"
$okButton.Location = New-Object System.Drawing.Point(100,80)
$okButton.Add_Click({
$form.Tag = $textBox.Text
$form.Close()
})
$form.Controls.Add($okButton)
# Show the form
$form.ShowDialog()
# Get the new hostname
$newName = $form.Tag
# Validate and apply
if ([string]::IsNullOrWhiteSpace($newName)) {
[System.Windows.Forms.MessageBox]::Show("Hostname cannot be empty. Operation cancelled.","Error",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Error)
exit
}
# Rename and restart
Rename-Computer -NewName $newName -Force
[System.Windows.Forms.MessageBox]::Show("Hostname changed to '$newName'. Restarting now.","Info",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Information)
Start-Sleep 10
Restart-ComputerInstall.ps1 - it will handle and execute what is mentioned in RenameComputer.ps1
[CmdletBinding()]
Param(
[Parameter(Mandatory = $False)] [string] $Script = "RenameComputer.ps1",
[Parameter(Mandatory = $False)] [Int32] $SessionID = 1
)
Process {
# Start logging
if (-not (Test-Path "$($env:ProgramData)\MrBSOEWay\Rename")) {
Mkdir "$($env:ProgramData)\MrBSOEWay\Rename"
}
Start-Transcript "$($env:ProgramData)\DevicMrBSOEWayie\Rename\Launch.log"
# Since ServiceUI.exe is 64-bit, we want it to run the 64-bit PowerShell.exe, passing in the script path
$psExe = "$($env:WINDIR)\System32\WindowsPowerShell\v1.0\powershell.exe"
& .\ServiceUI.exe -session:$sessionID $psExe -ExecutionPolicy Bypass -NoProfile -File "$PSScriptRoot\$Script"
# Create a tag file just so Intune knows this was installed
Set-Content -Path "$($env:ProgramData)\MrBSOEWay\Rename\Completed.ps1.tag" -Value "Installed"
Stop-Transcript
}Detect.ps1 - is just the detection file once the app installs:
if (test-path "C:\ProgramData\MrBSOEWay\Rename\Completed.ps1.tag"){"Installed"} ServiceUI.exe - you can grab the installer from Microsoft Deployment Toolkit (MDT)
The overall win32 app should look like this:

Package the win32 app with https://github.com/Microsoft/Microsoft-Win32-Content-Prep-Tool

Once the app is deployed, you will get this:
On Intune portal, name is:

App will kick off and start installing, prompt will appear:

Enter a new name and click OK.

Click "OK"

New name is WIN11-3D-01 after a restart

All scripts can be found here in my github repo.



Comments