We help IT Professionals succeed at work.
Get Started

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

Seth Garrett
Seth Garrett asked
Last Modified: 2017-03-21
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

Open in new window

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

Open in new window

Any help with this or may be even some pointers in the right direction would be appreciated. Many thanks.
Watch Question
The More I know, the more I don't know
Distinguished Expert 2020
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE