WinPE and .NET (Part 2): Creating and Editing Variables

In Part 1 we saw how to prepare a visual studio solution to be able to access the OSD Environment as well as obtain one specific or all of our task sequence variables. In there I linked a full solution that allows you to get all of those variables in a variety of file formats (txt, csv, xml, etc…).

In Part 2, I’ll be going over creating and setting Task Sequence variables via c# and .NET. If you have not read Part 1, please do. There are some important references and steps that need to be performed in order to properly access the environment.

As highlighted in Part 1, we need to:

  1. Extract and create our DLL (If you did this in Part 1, you can reuse this DLL)
  2. Create a solution setting the target framework to “.Net Framework 4”
  3. Reference the assembly in your solution

Now, just like we did in Par 1, we’ll need to create an instance of our Task Sequence environment:

ITSEnvClass TsEnvVar = new TSEnvClass();

If you recall the way we obtained variable names was using the GetVariables method of our Task Sequence Environment Instance. To get the value of a specific variable we accessed this by:


Creating and Setting variables is just as easy! In fact, its the exact same! With our Task Sequence Environment Instance (TsEnvVar):

TsEnvVar["VariableName"] = "VariableValue";

Really…that’s all there is to it! I’d like to say there’s more magic behind this but really there isn’t!

To help with your own implementations of this, I’ve created a helper class that can be used for getting and setting variables. While is is very basic, it does give a good ‘skeleton’ to work with. I’ve used this class in countless projects for both my company and other experiments/tests I’ve done. You can view this class here.

Up Next: In Part 3 I’ll go through a very quick and basic Task Sequence Wizard using WPF and C#!



WinPE and .NET (Part 1): Get Task Sequence Variables

When WinPE greeted me with the “.NET Framework” component I thought nothing of it…Then I decided to use it and couldn’t have seen more opportunity than when I first saw OSD.

I plan to make this a series of posts utilizing the .NET Framework with OSD ranging from the basics of Getting Task Sequence Variables, Setting Task Sequence Variables, and even adding these to make a visually rich WPF Task Sequence Wizard. I’ll focus on the C# language that Microsoft has made so easy to love. Although, keep in mind all these things can be done with C++ and .NET as well.

First things first we need the necessary dll in order to use the Task Sequence Environment.

Let me break and mention that there are 2 examples out there that explain this part a bit more than I will and may also provide more details for you:

Whether or not you’re following my blog or one of the two references there are a few prerequisites:

Creating the Necessary DLL

First we’ll need to crack open our boot media and get the tscore.dll file. We’ll use the handy DISM tool to do this:

  1. Get a SCCM Boot media ISO and mount it.
  2. Open the appropriate folder for your architecture and grab the tscore.dll file:
    1. <root>\SMS\bin\i386\tscore.dll
    2. <root>\SMS\bin\x64\tscore.dll
  3. Copy this locally.
  4. You’ll need to run the type library importer (tlbimp.exe) against this. As mentioned in John Socha-Leialoha’s blog above, this is specific to the .NET version you’ll be compiling in and installed to the boot media. A safe bet is .NET 4.0 (at least at this point in time) so we’ll go with that:
  5. “%WindowsSdkDir%\Bin\TlbImp” <PathFromStep3>\TSCore.dll”
  6. This will spit out a TsEnvironmentLib.dll file for us to import to Visual Studio.

Importing the DLL

For those of you familiar with adding references this should be fairly straight forward:

  1. Create your solution in Visual Studio and make sure to set the Target Framework to .NET Framework 4.
  2. In the Solution Explorer right-click References
  3. select Add Reference
  4. Click Browse and select the dll.
  5. Add the appropriate “using” to your code and you’re ready!

Getting Task Sequence Variables

There are a lot of variables in a Task Sequence and even more if you’re as customized as my solutions usually are. We’ll focus on the built-in variables for simplicity. Here are a few TechNet articles to become familiar with:

In the code we’ll need to get an instance of our Task Sequence Environment:

ITSEnvClass TsEnvVar = new TSEnvClass();

Now we could access any variable (if we know the name of it) which will return the value:


In order to get all of the variables we have set within our OSD environment, simply call the “GetVariables” method of our TSEnvClass instance which returns an array of our variable Names (not values).

object[] x = (object[])TsEnvVar.GetVariables();

You can then loop through the array from the snippet above to get all variables along with their values:

for (int i = 0; i < x.Length; i++)
    Console.WriteLine(x[i].ToString() + "\t:\t" + TsEnvVar[x[i].ToString()]);

And there we have it! For my own purposes I’ve created a nice little utility that will spit out all of the variables in a variety of formats. The solution is published to my GitHub here.

Up Next, Creating and Editing those variables!