PowerShell, Scripts

PowerShell – Rename Computers Dynamically according to type

Below is a PowerShell script to dynamically change a computer name based of the model of computer. It first checks the model to determine if the computer is a desktop or laptop, and renames the computer accordingly by appending a “D” or “L”.


#Get Current PC Name
$hostname = hostname

#Serial Number can also be used using below code inreplace of hostname
###  $hostname = wmic bios get serialnumber

#Get Model of System
$model = wmic computersystem get model

#If model is like "Laptop Model" add "L" to computername
IF ($model -like "Portege*" -or "Tecra*" -or "ProBook*") {

Rename-Computer -ComputerName "L"+$hostname }

#If model is like "Desktop Model" add "L" to computername
IF ($model -like "Veriton*" -or "Optiplex")  {

Rename-Computer -ComputerName "D"+$hostname }

This script could be utilized in a startup script or deployed through a deployment solution, such as SCCM.

An alternate to using the model value, is to use the ChassisType under the systemenclosure query.

This leverages the pre determined computer chassis values found here -> https://technet.microsoft.com/en-us/library/ee156537.aspx


#Get Current PC Name
$hostname = hostname

#Serial Number can also be used using below code inreplace of hostname
###  $hostname = wmic bios get serialnumber

# Gets Chassis Type - Chassis type references - https://technet.microsoft.com/en-us/library/ee156537.aspx
$chassis = wmic systemenclosure get ChassisTypes

#If chassis type is equal to "9 (Laptop)" or "10 (Notebook)" add "L" to computername
IF ($chassis -eq "{9}" -or "{10}*") {

Rename-Computer -ComputerName "L"+$hostname }

#If chassis type is equal to "3 (Desktop)" or "10 (Low Profile Desktop)" or "6 (Mini Tower")  or "7 (Tower)" add "D" to computername
IF ($chassis -eq "{3}" -or "{10}*" -or "{6}" -or "{7}") {

Rename-Computer -ComputerName "D"+$hostname }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s