This blog post will explain how you can dynamically assign machine variables to a computer object in a ConfigMgr 2012 using a simple PowerShell script.

My starting block was the code featured at

I wanted to be able to enter an array of variables and loop the code based on the number of variables in the array.

Download details

You can download the Add-MachineVariables.ps1 script from the Technet Gallery here


The script was developed to enable the storing of Machine Variables against computer object, particularly domain details. These variables would come in handy to set domain join against a specific device and therefore allow for cross-forest details to be entered dynamically at build time, using one TS step in one Task Sequence.

The script requires the Machine Variable ‘Name’ and ‘Values’ to be added prior to execution against the following:

  • $ComputerVariablesName
  • $ComputerVariablesValue

For the script to store the variables back to ConfigMgr, the number of entries in $ComputerVariablesName & $ComputerVariablesValue must match.

Here’s an example of the details that could be added:

$ComputerVariablesName = @("OSDDomainName", "OSDDomainOU")
$ComputerVariablesValue = @("", "LDAP://CN=Computers,DC=Contoso,DC=com")

The script will check to ensure that the number of items in each array match otherwise you will be prompted with a warning.


To execute the script run the following command:

./Add-MachineVariables.ps1 -ComputerName HostName-01 -SiteServer ConfigMgr-01 -SiteCode CMR

Note that you must adjust the details in the command to reflect objects in your ConfigMgr 2012 environment.

  • ComputerName – Specify the Host Name of the device.
  • SiteCode – Specify the site code of the ConfigMgr site.
  • SiteServer – Specify the site server name.


I will run the script against the Computer ‘App-v01’. A quick check on the computer object in ConfigMgr and I can see that the currently no variables are assigned to that device.


The script is executed with the following set for Name and Value:

$ComputerVariablesName = @("OSDDomainName", "OSDDomainOU")
$ComputerVariablesValue = @("", "LDAP://OU=Desktops,OU=ConfigMgr,DC=internal,DC=sccmsolutions,DC=com")


The script has run successfully and the computer object is now populated with the relevant machine variable ‘Name’ and ‘Values’.


I can now use these dynamic variables in my Task Sequence at build time to dynamically add to a domain or OU.