The only character that cannot ever appear in a user defined batch environment variable name is =
. The SET statement will terminate a variable name at the first occurrence of =
, and everything after will be part of the value.
It is simple to assign a variable name containing a :
, but the value cannot normally be expanded except under specific circumstances.
When extensions are enabled (default behavior)
The colon is part of search/replace and substring expansion syntax, which interferes with expansion of variables containing colon in the name.
There is one exception - if the :
appears as the last character in the name, then the variable can be expanded just fine, but then you cannot do search and replace or substring expansion operations on the value.
When extensions are disabled
Search/replace and substring expansion are not available, so there is nothing to stop expansion of variables containing colons from working just fine.
@echo off
setlocal enableExtensions
set "test:=[value of test:]"
set "test:more=[value of test:more]"
set "test"
echo(
echo With extensions enabled
echo -------------------------
echo %%test:%% = %test:%
echo %%test::test=replace%% = %test::test=replace%
echo %%test::~0,4%% = %test::~0,4%
echo %%test:more%% = %test:more%
setlocal disableExtensions
echo(
echo With extensions disabled
echo -------------------------
echo %%test:%% = %test:%
echo %%test:more%% = %test:more%
--OUTPUT--
test:=[value of test:]
test:more=[value of test:more]
With extensions enabled
-------------------------
%test:% = [value of test:]
%test::test=replace% = :test=replace
%test::~0,4% = :~0,4
%test:more% = more
With extensions disabled
-------------------------
%test:% = [value of test:]
%test:more% = [value of test:more]
See https://stackoverflow.com/a/7970912/1012053 for a complete description of exactly how variable expansion works.