troubleshooting Question

PS Script to rename computer based on current logged in user and device type

Avatar of Seth Garrett
Seth Garrett asked on
6 Comments1 Solution151 ViewsLast Modified:
Hi guys,

Can someone please help with the powershell script for changing the computer name based on current logged in user and device type.
PS: We don't have AD/DC - nothing on prem. All standalone PC's connected to AzureAD. So we will push this script down to the machine using the our MDM tool.

For example:
If the current logged in user on the PC is AzureAD\JohnSmith and Device Type is a Laptop, then the computer should be renamed as JohnSmith-L

Basically just get the parts in bold above and concatenate them and then set it as the computer name.

I've got a few bits together for getting the Name and Device Type. Just cant get my head around how to set the name as desired.

To get the current logged in user name:
$(Get-WMIObject -class Win32_ComputerSystem | select username).username

To get the device type:
function check-chassis {
         Write-Output "Processing $_ which is a:-"
         $computer = "$_"
         $chassis = Get-WmiObject win32_systemenclosure -computer $computer | select chassistypes
         if ($chassis.chassistypes -contains '3'){Write-Output "Desktop"}
         elseif ($chassis.chassistypes -contains '4'){Write-Output "Low Profile Desktop"}
         elseif ($chassis.chassistypes -contains '5'){Write-Output "Pizza Box"}
         elseif ($chassis.chassistypes -contains '6'){Write-Output "Mini Tower"}
         elseif ($chassis.chassistypes -contains '7'){Write-Output "Tower"}
         elseif ($chassis.chassistypes -contains '8'){Write-Output "Portable"}
         elseif ($chassis.chassistypes -contains '9'){Write-Output "Laptop"}
         elseif ($chassis.chassistypes -contains '10'){Write-Output "Notebook"}
         elseif ($chassis.chassistypes -contains '11'){Write-Output "Hand Held"}
         elseif ($chassis.chassistypes -contains '12'){Write-Output "Docking Station"}
         elseif ($chassis.chassistypes -contains '13'){Write-Output "All in One"}
         elseif ($chassis.chassistypes -contains '14'){Write-Output "Sub Notebook"}
         elseif ($chassis.chassistypes -contains '15'){Write-Output "Space-Saving"}
         elseif ($chassis.chassistypes -contains '16'){Write-Output "Lunch Box"}
         elseif ($chassis.chassistypes -contains '17'){Write-Output "Main System Chassis"}
         elseif ($chassis.chassistypes -contains '18'){Write-Output "Expansion Chassis"}
         elseif ($chassis.chassistypes -contains '19'){Write-Output "Sub Chassis"}
         elseif ($chassis.chassistypes -contains '20'){Write-Output "Bus Expansion Chassis"}
         elseif ($chassis.chassistypes -contains '21'){Write-Output "Peripheral Chassis"}
         elseif ($chassis.chassistypes -contains '22'){Write-Output "Storage Chassis"}
         elseif ($chassis.chassistypes -contains '23'){Write-Output "Rack Mount Chassis"}
         elseif ($chassis.chassistypes -contains '24'){Write-Output "Sealed-Case PC"}
         else {Write-output "Unknown"}
"localhost" | check-chassis

Any help with this or may be even some pointers in the right direction would be appreciated. Many thanks.
David Johnson, CD
The More I know, the more I don't know
Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros