New Environmental Variable

Posted on 2010-09-17
Last Modified: 2012-05-10
I am wanting to create a new environmental variable named something like shortname and have the value be %COMPUTERNAME:~0,-2%.

I want to be able to type echo %shortname% and see the computer name minus the last two characters.

I tried creating the environment variable by entering in "%COMPUTERNAME:~0,-2%." as the value (under environmental variables in Advanced system settings but it literally echoed "%COMPUTERNAME:~0,-2%."

How can I achieve this? My client OS is Windows 7. My end goal is to be able to use this variable in group policy preferences. I am wanting to deploy a registry key where the value of the registry key is the computer name minus the last two characters of the name.
Question by:Joseph Moody
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +3

Expert Comment

ID: 33701254
set <new varialbe name> = %COMPUTERNAME:~0,-2%

should do the trick
LVL 55

Expert Comment

by:Bill Prew
ID: 33701550
You can do the following at a command prompt:


and that will set a new variable name ShortName into the permanent system variables.  But keep in mind that it will only resolve the computername at the time you run the STX command, if you change the computername later it will not automatically be reflected in the system variable.

I tried a few tests, it didn't look like you could create a new system variable referencing %computername% and have it get the value of computername into the new variable.

You could add a line to the autoexec.bat file with the setx in it, that way each time the system restarts it would at least be updated.

LVL 43

Expert Comment

by:Steve Knight
ID: 33701974
Wow, when did SETX arrive?!!? Wow looks useful with the /f /a /d params etc.

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

LVL 55

Expert Comment

by:Bill Prew
ID: 33703308
It was originally in some of the resource kits, I know at least as far back as Windows 2000.  It's now in the base O/S as of Windows 7 also I believe.

LVL 43

Expert Comment

by:Steve Knight
ID: 33703905
Ah thats why it was on this vanilla Win7 machine then.  Will have to have a play with it!
LVL 13
ID: 33704152
Hey All,

It appears to be that the only reason the requester wants to do this is so that he can set a registry key to be used in GPOs later..
 Therefore We should consider doing the addition to or substitution of creating the environment variable as the reg key is the ultimate solution for him.
The normal reg key where the Computer name is located is thus:
  • Key:    HKEY_LOCAL_MACHINE\system\currentcontrolset\control\computername
  • Value:    ComputerName
  • Type:    REG_SZ
  • Data:    [The name of the computer]
Since author's ultimate goal would best be served by adding this value minus the last two characters to every machine.  We could, using a logon script or GPO have this key created  at the same location:
  • Key:    HKEY_LOCAL_MACHINE\system\currentcontrolset\control\computername
  • Value:    ShortName
  • Type:    REG_EXPAND_SZ
  • Data:    %ComputerName:~0,-2%
When evaluated by a GPO this Registry value will always evaluate as the current computer name minus the last two characters, and you aren't creating a hard entry, it will change if you change the name of the computer to come other value without you having to re-run any scripts.

Thats the beauty of the string expansion feature of the Windows Registry ^^

To add this value run this:
REG ADD HKLM\system\currentcontrolset\control\computername /v Shortname /t REG_EXPAND_SZ /d ^%ComputerName:~0,-2^%
Then a GPO will be able to reference this value, and it will not be a string which could be differant than the actual value of the computer.

Furthermore, as Bill deftly pointed out above you can set this value as a system variable at any time by using SETX and either using %ComputerName:~0,-2% or in fact querying the registry for the value we've set here. ^^
REG ADD HKLM\system\currentcontrolset\control\computername /v Shortname /t REG_EXPAND_SZ /d ^%ComputerName:~0,-2^%

Open in new window

LVL 13
ID: 33704360
also @ Bill re: how to set the system variable and evalutate it correctly every time:

I don;t think this is particularly useful in this instance but there may be other times you'd want to use this. So one thing you can do is set and echo the variable like this if need be

SETX Shortname=^%ComputerName:~0,-2^%


ECHO %ShortName%

This will echo the literal string: %ComputerName:~0,-2%
CALL ECHO %ShortName%

This will echo the computer's actual name minus the last two characters.

Since SET is Transient and SETX is permanent we'd run the SETX above and then on each system start-up you could run

CALL SET "ShortName=%ShortName%"

However in this case when it comes to the system variable we're all trying to reinvent the wheel.

If the process is a batch script it can just set short name every time it runs on a remote machine or simply just evaluate %ComputerName:~0,-2% when it needs to reference shortname.

If we're going to be setting the reg key for a GPO to evaluate, I think it's best to use the registry expansion method I posted above because that will be evaluated as a string equal to the computer's ACTUAL name minus the last two characters every time.

LVL 26

Expert Comment

ID: 33704754
Wow.  another new command SETX to look into.  I don't have it natively on my XP box at work but will look into how to get it if possible.

This would have made life so much easier in so many ways.  :)

Thanks again Bill
LVL 22

Author Comment

by:Joseph Moody
ID: 33715590
Thank you for helping me so far! QCubed got me thinking and I would like to show what exactly I am wanting to do.
I have a registry key at HKLM\SOFTWARE\Policies\NetSupport\PCICTL\ConfigList\Standard that is named Startup\\BrowsePrefix. It is a REG_SZ
Netsupport is a remote desktop program that we use.  When the program starts up, it checks that BrowsePrefix key for a computer name prefix. I am wanting that key to have the computer name (minus the last two characters) in the data field of that key.
Right now, I have a ton of policies with that manually specify that key. I am wanting to go down to one policy that dynamically sets that key.
LVL 13
ID: 33716010
Add this to a startup script:

REG ADD "HKLM\SOFTWARE\Policies\NetSupport\PCICTL\ConfigList\Standard that is named Startup" /v "BrowsePrefix" /t REG_SZ /d "%ComputerName:~0,-2%"


LVL 22

Author Comment

by:Joseph Moody
ID: 33716035
That worked great! Could I do that with group policy preferences?
LVL 13

Accepted Solution

Ben Personick (Previously QCubed) earned 500 total points
ID: 33716193
Sorry here is the full post

Add this to a start-up script:

REG ADD "HKLM\SOFTWARE\Policies\NetSupport\PCICTL\ConfigList\Standard that is named Startup" /v "BrowsePrefix" /t REG_SZ /d "%ComputerName:~0,-2%" /f


Use this to be a bit more rock-solid in case you ever change the computer name but don't have the script running anymore:

REG ADD "HKLM\SOFTWARE\Policies\NetSupport\PCICTL\ConfigList\Standard that is named Startup" /v "BrowsePrefix" /t REG_EXPAND_SZ /d "^%ComputerName:~0,-2^%" /f
LVL 13
ID: 33716399

Yes, you can set this to run as a start-up or logon script  via the associated GPOs.  Honestly you should only need to set this to be a start-up script, though you could set it as a logon script which woudl refresh the value whenever someone logs onto the computer but that requires they be local administrators when running the script so less useful, for sure.

(In the case of the second option where we change the key to be a REG_EXPAND_SZ it only needs to be done once per machine (assuming the key does not get over-written by your program.. so perhaps even still a start-up script woudl be most appropriate  ^^))

First you'll need to take the command you prefer REG_SZ or REG_EXPAND_SZ and put it in a batch file, open Notepad (don't use a rich text editor like word or wordpad!), put "@ECHO OFF" (Without the quotes)as the first line, put the second line to the the one you prefer from my expample, and save the batch file as something you will remember the purpose of (Lets say "SetRDPShortName.bat" -without thee quotes-) under "\\\Netlogon\SetRDPShortName.bat" (quotes again not needed ^^).

Once you've done that:

(Lifted from ^^):
"To configure the computer startup and shutdown scripts, start the Group Policy snap-in, expand Computer Configuration, expand Windows Settings, click Scripts (Startup/Shutdown), and then in the right pane, double-click the script that you want to configure. These scripts run on the Local System account."

Then simply browse to the script you just saved or type the path into the GPO "\\\Netlogon\SetRDPShortName.bat" (here you may want to use quotes around the path, not sure)..

save it, apply it to an OU or the domain. select "Enforce".
REG ADD "HKLM\SOFTWARE\Policies\NetSupport\PCICTL\ConfigList\Standard that is named Startup" /v "BrowsePrefix" /t REG_SZ /d "%ComputerName:~0,-2%" /f

Open in new window

LVL 22

Author Closing Comment

by:Joseph Moody
ID: 33716470
That did it!!!! This is the coolest thing ever!

Thank you for being able to come up with a solution even though my first question was not here specific enough!!!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question