Using Batch to change Computer Name and UserName based off of their current Computer Name?

1.1k views Asked by At

I'm writing a batch that I'll remotely place in the all users startup folder using Desktop Central 9 /PsExec. We are going to start using a naming convention as part of an initiative to overhaul computer management. This batch needs to find what the current PC Name is and then change it based off of what I will have manually defined in the batch itself. It would also be nice to be able to then also change the users name based off of what computer name is initially found. All computers use Windows 7 and above. I wrote a quick batch of what I think it should look like, minus the user name modification, but there are nuances here and there I must be getting wrong. The "If" statement is my weakness with batch, but I know it will be extremely efficient in this case. Can anyone correct what I'm doing wrong here? Much appreciated, this will help us A LOT!

SET WMIC GET COMPUTERNAME %%CURRENT%

If %%CURRENT% = THOMAS-PC then REG ADD HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName /v ComputerName /t REG_SZ /d TGZ-DELL-001 /f

If %%CURRENT% = CHARLES-PC then REG ADD HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName /v ComputerName /t REG_SZ /d TGZ-DELL-002 /f

If %%CURRENT% = MATHEW-PC then REG ADD HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName /v ComputerName /t REG_SZ /d TGZ-DELL-003 /f

1

There are 1 answers

0
RGuggisberg On
  1. %%CURRENT% is incorrect. Use %CURRENT%
  2. IF statements do not have a "then". Use parens if you have multiple lines in IF statement.
  3. Use = to SET and == to compare
  4. Use quotes as shown in my example so that the spaces, etc don't cause problems
  5. No need for WMIC in this case.

How about something like this? I added /I to make the compare case insensitive. Leave that out if you wish... but then your compare would miss Thomas-PC, etc.

**** CAUTION **** Untested! Of course you will test test on a test machine before messing up the registry on a working machine :)

@echo off
set "RegCmd=REG ADD HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName /v ComputerName /t REG_SZ /d"

echo(this computer is %ComputerName%
if /I "%ComputerName%"=="THOMAS-PC" %RegCmd% TGZ-DELL-001 /f
if /I "%ComputerName%"=="CHARLES-PC" %RegCmd% TGZ-DELL-002 /f
if /I "%ComputerName%"=="MATHEW-PC" %RegCmd% TGZ-DELL-003 /f