batch-filerenamecommand-promptcomputer-name

Create a script to rename computer using naming scheme


I have this script for naming a machine. It is for an automatic rename after I successfully re-imaged a machine. To save time when doing 40 or 50 at a time.

SETLOCAL ENABLEDELAYEDEXPANSION
@ECHO OFF
SET count=1
SET campus=JOB

FOR /F "tokens=* USEBACKQ" %%F IN (`wmic bios get serialnumber`) DO ( 
SET serialnumber!count!=%%F
SET /a count=!count!+1
)

SET pcname=%campus%-%serialnumber2%

ENDLOCAL

WMIC computersystem where caption=%computername% rename %pcname%

pause

I have several different campuses and with either a 3 or 4 character name scheme and using the serial number of the device. I was wondering if there was a way to find out if the %pcname% is less than or equal to 15 characters, if not, then cut off the last digits to equal 15 characters? Since Microsoft only allows 15 characters for a computer name, then this script should account for it. In theory.


Solution

  • Just :

    set pcname=%pcname:~0,15%
    

    If its longer it will be cutted if not it will stay as it is

    In your code :

    SETLOCAL ENABLEDELAYEDEXPANSION
    @ECHO OFF
    SET count=1
    SET campus=JOB
    
    FOR /F "tokens=* USEBACKQ" %%F IN (`wmic bios get serialnumber`) DO ( 
    SET serialnumber!count!=%%F
    SET /a count=!count!+1
    )
    
    SET pcname=%campus%-%serialnumber2%
    
    ::
    SET pcname=%pcname:~0,15%
    SET pcname=%pcname: =%
    ::
    
    
    WMIC computersystem where caption=%computername% rename %pcname%
    
    pause
    

    That should do the trick !