Archive

Archive for the ‘Windows’ Category

Automatically setting permanent Environment Variables on Windows

August 26, 2013 Leave a comment

More than once I needed to define a permanent Environment Variable on a Windows machine – mostly as part of some automatic installation process. E.g. – like the Java installer sets the JAVA_HOME during installation.

Windows has a very simple utility to do so – SETX.

# Set the variable HELLO with the value "WORLD" for the current user
SETX HELLO WORLD
# Set the variable HELLO with the value "WORLD" for all users (System)
SETX HELLO WORLD /m

Note that SETX is different from SET – invoking SETX as part of a shell script will not make the variable available for that shell script.

Reading material
Official Documentation – http://technet.microsoft.com/en-us/library/cc755104.aspx
Non-Official Documentation – http://ss64.com/nt/setx.html

Changing a service’s startup policy via CLI

April 16, 2012 Leave a comment

I recently needed to automate an installation of a service but ran into a problem that it was starting automatically by default (which we did not want).

So I found a simple CLI to change this behavior –

reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\my-service-name /v Start /t REG_DWORD /d 3 /f

What it does is:

  1. Go to the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\my-service-name entry in the registry
  2. Sets the value of Start (/v Start) to be the 32bit value (/t REG_DWORD) of 3 (/d 3)
  3. It overrides the current value if exists (/f)
All valid values are:
  • 2 = Automatic
  • 3 = Manual
  • 4 = Disabled
I have to give credit to this Microsoft article: http://support.microsoft.com/kb/248660

Manipulating the values of environment variables in windows batch files

April 3, 2012 Leave a comment

Lets say you have an environment variable that contains a certain value and you want to change it in some manner.
Example:

set AAA=abc_123
set BBB=%AAA:~0,3%%AAA:~4,3%
echo %BBB%

The output will be abc123
The syntax is: %var:~<offset>,<count>%
Examples:
echo %AAA:~0,1% – will print “a” – Start at offset 0 and count 1 character.
echo %AAA:~1,2% – will print “bc” – Start at offset 1 and count 2 characters.
echo %AAA:~3,3% – will print “_12” – Start at offset 3 and count 3 characters.
echo %AAA:~1,-2% – will print “bc_1” – Start at offset 1 and count 2 characters from the end.

Another option is

set AAA=abc_123
set BBB=%AAA:abc=ABC%
echo %BBB%

The output will be ABC_123
The syntax is: %var:<string to find>=<alternate value>%
Examples:
echo %AAA:a=A% – will print “Abc_123”.
echo %AAA:_=-% – will print “abc-123”

There are more options – just run help set on the command line to find them out.

%d bloggers like this: