Set output variable in a Powershell VSTS Build Task

Currently I am building a pipeline that performs some actions on Azure. For example creating a Azure Storage Account. When doing this, I want to return some values from my Powershell script back into a variable of my Build Pipeline, so I can use this in another task.

After some searching I finally found out how to do this. I will explain with an example

First create an empty Build Definition and add 2 Powershell Tasks to it


In the first inline Powershell task add one line to write the initial value and the second line to override the value.

Write-host $env:OutputVar

Write-Output ("##vso[task.setvariable variable=OutputVar;]UpdatedValueInScript")

You can set the variable by using the VSTS embedded functions.

##vso[task.setvariable variable=OutputVar;]UpdatedValueInScript

The Write-Output acts as an executor for this

The second task just outputs the variable again

Write-host $env:OutputVar

After that run the build


Happy building!


3 Responses to “Set output variable in a Powershell VSTS Build Task”

  1. Hi Rene,

    there is also another way of doing it, through a cmdlet present in one of the modules that do ship with the agent. Some time ago I wrote about it here

    It is also available in the next task library,


  2. Great solution, thank you for an excellent article.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: