With the following ComObjects you can easily read/ write and create task sequence variables.
To use TS variables in a powershell script use the following lines of code;
To initialize the ComObject while in TS:
$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
To display a TS variable value use:
To write a value to a TS variable use :
$tsenv.Value("OSDComputerName") = "PC_MyComputerName"
Source information was found from here.