I'm trying to retrieve a MDT task sequence variable inside a Powershell script, then use that variable in a Powershell function. My problem lies in that the variable I want to retrieve contains a Powershell-variable that will not resolve properly. This is how it is setup:

  1. In MDT I use the "Set Task Sequence Variable"-task with the name "TS-Destination" and value "$($env:systemdrive)\dep" (without the quotes).

  2. A PowerShell-script is added through the "Run Powershell Script"-task and is run as "%scriptroot%\Script.ps1" (without quotes) with arguments "-Source 'Z:\someprogram'" (without doublequotes). In the Powershell-script the part where my problem lies is like this:

Beginning of script





$tsenv = New-Object -ComObject Microsoft.SMS.TSEnvironment

If ($Destination -eq "") {

$Destination = $tsenv.Value('TS-Destination')


Copy-Item $Source -Destination $Destination -Recurse

End of script

First of all I would like to say that the script works if I set the variable manually inside the script, but I would like it to be easily changeable and flexible depending on the preference of the user(the choice of using a parameter or TS Variable).

How I figured this would work is it would get the value from the "TS-Destination"-variable and use it in the $destination-variable (and resolve it), but when the script runs it is unable to resolve the variable. It gets an error that a drive with the name '$($env' does not exist (which is an error from Copy-Item because the variable did not resolve). It seems that it stops reading the variable value after the colon, but I am unable to figure out why.

When troubleshooting and setting the value manually (by using $tsenv.Value('TS-Destination') = "$($env:systemdrive)\dep" and then using that works and properly resolves the variable. I've tried various methods to escape characters and set the variable, but nothing seems to work. Does the fact that the variable is defined as a [string]-parameter prevent something like this from working?

I need this value to be able to accept variable-format, because the script is being used in multiple parts of the Task Sequence, and the systemdrive changes during the deployment process. I could set the variable multiple times during the Task Sequence, but that would complicate the Task Sequence more than necessary, and I would like the complicated parts to be within the script :)

So the main question is:

How do I get this working properly?

  • 1
    Does passing "-source $env:systemdrive\dep" give the same error as "$($env:systemdrive)\dep" ? – Bin Dec 17 '14 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.