Create custom SCCM Task Sequence variables using PowerShell

Last week I worked on BIOS updates for our Dell desktops and our HP laptops in Windows PE. The result was lots of new tasks in the task sequence with WMI queries to determine the model. After a BIOS update the machine needs a reboot, but doesn’t when already updated. To keep things fast and clean, I needed to find a solution to save time. I solved this with custom Task Sequence variables using PowerShell. Read along to find out how.

Fix it with a one-liner

I already had my tasks built using the ‘Run a command line’ action. To keep things clean and simple, I wanted to integrate my custom variables within the same task. This is where PowerShell comes in with just a one-liner that you can add alongside your initial commands.

powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& {$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment; $tsenv.Value('BIOSUpdated') = 'True'}"

Note: The availability of PowerShell in Windows PE is required. Learn how to add PowerShell to Windows PE on the Microsoft Docs.

Let’s break it down

We need to call the PowerShell executable. Also, let PowerShell know what- and how it needs to run the script.

powershell.exe -NoProfile -ExecutionPolicy Bypass -Command
-NoProfileMakes sure the PowerShell profile isn’t loaded
-ExecutionPolicy BypassBypasses the execution policy of unsigned scripts so we don’t need to sign our own script
-CommandThe code we want to execute through PowerShell

Create task sequence variables using PowerShell

We will create a variable called $Value and declare the actual value we want to assign to our tasks sequence variable. In my case I will use True, because I want to know if the task sequence executed a BIOS update.

Secondly, we will create a new object, which is the task sequence variable. We will tell that task sequence variable to name it BIOSUpdated and assign the variable $Value that stands for True.

$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment; $tsenv.Value('BIOSUpdated') = 'True'

The benefit of this method, is that you can query your Restart into Windows PE step to not run when this task sequence variable with this specific value exists. As a result, you will see a shorter deployment time and fewer restarts.

add condition task sequence variable

Note: There is no limit in the amount of task sequence variables you create.

Wrapping things up

When working in Enterprise environments, time is precious. The deployment of devices does not go with just a few but in hundreds or thousands, so every second you save in your configuration does actually improve deployment time.

You’ve learned how to create task sequence variables with only one line of code in PowerShell. Task sequence variables will give you the ability to trigger (or not to trigger) steps further down in your task sequence.

Do you have any questions or comments? Please feel free to get in touch.

Leave a Reply

Your email address will not be published.