0Batch script to rename PC based on reserved DHCP name

I use WDS on a stan­dalone serv­er to set up new PCs and to reim­age PCs when they have ended up mangled bey­ond repair. With a good num­ber of PCs in my own house it is use­ful to reim­age them oca­sion­ally when swap­ping or upgrad­ing hard­ware. Most of this pro­cess is now auto­mated by an unattend.xml file but one step the unat­ten­ded pro­cess does­n’t seem to sup­port is renam­ing PCs back to their ori­gin­al name.

All of my PCs have stat­ic DHCP reser­va­tions with their cor­rect host­name (which is also registered in DNS) so I figured there must be a way to pull this inform­a­tion and use it to rename them. After much fid­dling about I have pro­duced a batch script that will do this (when run as admin).

Simple paste the fol­low­ing code into a .cmd file and run it as part of your unat­ten­ded pro­cess (I describe mine below)

I call my script from the <FirstLogonCommands> sec­tion of ImageUnattend.xml.

To run it as admin I use a little util­ity (called elevate.exe) that does this (com­mands run dur­ing setup can elev­ate without pop­ping up a prompt!). You can get it from it’s ori­gin­al source

The final piece of the puzzle for my setup is that I modi­fy the win­dows install image to include an exe file and a little poweer­shell script — these allow the copy­ing of all the oth­er installers from the net­work. The exe file is ‘streams’ which is a sys­in­tern­als (now part of Microsoft) util­ity to remove the “this file came from the inter­net are you sure you want to run it” warning.

I also have the fol­low­ing script as a .ps1 file

My first 4 Logon­Com­mands are then as follows…

Think we've missed something? Let us know by commenting below. If you would like to subscribe please use the subscribe link on the menu at the top right. You can also share this with your friends by using the social links below. Cheers.

Leave a Reply