Using Environment Variables in Visual Studio 2013 and TFS 2013

Posted by: Gouri Sohoni , on 8/21/2014, in Category Visual Studio
Views: 60186
Abstract: Using Visual Studio 2013 and Team Foundation Server (TFS) 2013 Build Service, in this article, we will view various ways using which environmental variables can be accessed with Build Definition.

In this article, we will view various ways using which environmental variables can be accessed with Build Definition. We will be using Visual Studio 2013 and Team Foundation Server 2013 Build Service for that. Sometimes we need to use the path to the source folder or the folder where the build binaries are getting dropped. Such kind of variables are termed as TF_BUILD environment variables.

Environmental Variables are a set of values that can help in running build definitions. These are created and set by the Team Foundation Build Service before the actual build workflow is invoked. We may require these variables for various reasons. For example, we may want to use InvokeProcess activity which in turn will call another process. Our need is to send the drop location of the binaries to it. Another example can be that we want to add another section in build summary where we provide the build reason and build number. We can use environmental variable to achieve that and send it to WriteCustomSummaryInformation activity.


You will find out more information about InvokeProcess and WriteCustomSummaryInformation in my next upcoming article.

TFS 2013 Build Service supports the following environmental variables.


All these names are self-explanatory.

We will find out 3 ways using which we can access these variables

  • Using PowerShell Script for variables
  • Using the variables in MSBuild by setting parameters
  • Using custom build process for environmental variables

Use Environmental variables in PowerShell script

Build variables can be used in script by using the same names I have provided above. Follow this procedure to access build variables with their names in the PowerShell scripts.

Open any existing project that you want to build.

1. Add a new item to project of type text file to “PreBuildScript.ps1”

2. Add these lines as commands to the script

Write-Host 'This is pre build script'
Write-Host "The build definition name is " $Env: TF_BUILD_BUILDDEFINITIONNAME
Write-Host "Drop Location is " $Env: TF_BUILD_DROPLOCATION
Write-Host 'pre build script over'
Write-Host '*******************************************************************'

3. Add another item named “PostBuildScript.ps1” to the project with following commands:

Write-Host 'This is coming from post build script'
Write-Host "Build Number is " $Env: TF_BUILD_BUILDNUMBER
Write-Host '#################################################'

4. Save both the files and check-in

5. Create a build definition with name [Project Name] – Run Script

6. Keep manual trigger

7. Provide the build controller name if not already selected

8. You need a shared folder on the Build Server as drop location. Provide the path to the TFS shared folder name for staging location as follows:



9. From Process tab, remove the automated tests to be executed, if you don’t want to execute any automated tests


10. Optionally provide following Build number format in Process tab 5.Advanced



11. Provide the path to pre and post build scripts and specify -verbose for verbosity as shown. We will be able to see the output of the scripts in Diagnostics Logs of build.


12. Save the build definition and queue the build

13. Once the build is successful, right click on it in Build Explorer and select Open in browser


14. Select Diagnostics tab. Scroll down until you find the following



Use Environmental variables in MS Build

When you are using MS Build activity to compile the code, you can pass the arguments to it as and when required. This can be either using a default template or a custom template. Once the parameter is available to MSBuild process, it can be accessed and used in target as required.

I am using the example of setting build number as argument


After saving the build definition and executing it, we can see the parameter appearing.


Below is the screenshot of build diagnostics without parameter passing


Using Environmental Variables with Build Process

We can pass the environmental variables to any activity in either default process or custom process. In order to achieve this, we need to use the activity named GetEnvironmentalVariable<T>

1. Create either a variable or an argument to pass the value to any activity

2. Argument can be passed as a parameter while either editing a build definition or queuing it or at both times, if set so. I am going to add these in a Sequence activity

3. I have given name as “dropLocation” to it

4. Provide the configurations to the activity as follows:



is available in assembly “Microsoft.TeamFoundation.Build.Activities.dll”

5. Observe the result is passed to the variable or argument already declared. This can be used in another activity as and when required.

6. In this case, we will add a WriteBuildMessage to show the value. It can be used as a mediator between multiple activities. The complete workflow looks as below:


7. The Diagnostics tab for the successful build will show the details of the drop location.



In this article we discussed how environmental variables can be used with Visual Studio 2013 and Team Foundation Server 2013 Build Service. I hope you enjoyed reading this article.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Gouri is a Trainer and Consultant on Azure DevOps and Azure Development. She has an experience of three decades in software training and consulting. She is a graduate from Pune University and PGDCA from Pune University. Gouri is a Microsoft Most Valuable Professional (MVP)  - Developer Technologies (Azure DevOps), Microsoft Certified Trainer (MCT) and a Microsoft Certified Azure DevOps Engineer Expert. She has conducted over 150 corporate trainings on various Microsoft technologies. She is a speaker with Pune User Group and has conducted sessions on Azure DevOps, SQL Server Business Intelligence and Mobile Application Development. Gouri has written more than 75 articles on Azure DevOps, TFS, SQL Server Business Intelligence and SQL Azure which are published on and You can connect with her on LinkedIn.

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by John Bernaldo on Thursday, August 21, 2014 11:10 PM
Hi, the facts discussed was really interesting. I want more articles to make informative.
Comment posted by farrukh on Tuesday, October 21, 2014 12:47 AM
Very nice and detailed article. But I was looking for Creating my own environment variables during build. E.g. I'm extracting and concatenating buildnumber with revision and other info and wanted to save it in another variable. And at the end of MSBuild session, I would like to use this variable to rename some files.

Is it possible to create new env. variables in TFS for my purpose?