Setting Environment Variables in a Task Sequence
Michael Murgolo has a nice post over on The Deployment Guys blog.
Read the full and download the example script here.
Some tools require setting an environment variable when they are used. For example, the User State Migration Tool has several that can be used for troubleshooting. One is discussed in the Ask the Directory Services Team blog post here. Unfortunately, the built-in MDT or ConfigMgr Task Sequence steps for capturing and restoring the user state don’t allow you to set environment variables.
Trying to set the environment variable using a script in a preceding step will not work. If you set an environment variable in the script (e.g. using the SET command in a command shell script) it will only be set for that script. The task sequencer parent process will not inherit the environment variable, so neither will subsequent steps. Setting a System (or master) environment variable will have the same issue. The task sequencer will not inherit the new master environment. However, if you restart the computer after setting the System environment variable then task sequencer will inherit the updated System environment variables.
So the process for using an environment variable with something like USMT is to have steps before the steps that run the tool (the user state capture and restore steps in this case) that set the variable in the System environment and then restart the computer. This is shown in the steps prefixed with Custom: in the sample MDT Lite Touch Task Sequence below.