XP_CMDSHELL doesn't work on user environmental variables


I am trying to run a batch file from xp_cmdshell.

It is being run in batch file which uses user created environment variables(user and system variables) and xp_cmdshell does not recognize those environment variable...

In sql client, if i give EXEC xp_cmdshell 'echo %PATH%'
Then it properly displays the content of path variable.

However, i create a env variable, say TEST and assign value as C:\Test and try to execute in xp_cmdshell
EXEC xp_cmdshell 'echo %TEST%'
Output is %TEST%

As suggested in forums, I have also restarted the sql server service. Still it is not working.

Any suggestions are appreciated.


%PATH% is a universal environment variable, only these types will be picked up by xp_cmdshell when you execute it. Any non-universal environment variable will have to be set in the batch that xp_cmdshell is running, and will not be durable/persisted between executions (you have to set them each time).