Utility: Batch script

Last Update: 5/4/2017

Team Services | TFS 2017 | TFS 2015 | Previous versions: XAML Build, Release

Run a Windows .bat or .cmd script and optionally allow it to change the environment.


Argument Description

Specify the path to the .bat or .cmd script you want to run. The path must be a fully qualified path or a valid path relative to the default working directory.

In Team Foundation Build, this directory is $(Build.SourcesDirectory).

Arguments Specify arguments to pass to the script.
Modify environment Select this check box if you want environment variable modifications in the script to affect subsequent tasks.
Working folder Specify the working directory in which you want to run the script. If you leave it empty, the working directory is the folder where the script is located.
Fail on standard error Select this check box if you want the build to fail if errors are written to the StandardError stream.
Control options


Create test.bat at the root of your repo:

@echo off
echo Hello World from %AGENT_NAME%.
echo My ID is %AGENT_ID%.
echo AGENT_WORKFOLDER contents:
echo Over and out.

On the Build tab of a build definition, add this step:

Utility: Batch Script

Run test.bat.

  • Path: test.bat


Where can I learn about batch files?

Using batch files

Where can I learn Windows commands?

An A-Z Index of the Windows CMD command line

How do I set a variable so that it can be read by subsequent scripts and tasks?

Define and modify your build variables in a script

Define and modify your release variables in a script

Q: I'm having problems. How can I troubleshoot them?

A: Try this:

  1. On the variables tab, add system.debug and set it to true. Select to allow at queue time.

  2. In the explorer tab, view your completed build and click the build step to view its output.

The control options arguments described above can also be useful when you're trying to isolate a problem.

Q: How do variables work? What variables are available for me to use in the arguments?

A: $(Build.SourcesDirectory) and $(Agent.BuildDirectory) are just a few of the variables you can use. See Variables.

Do I need an agent?

You need at least one agent to run your build or release. Get an agent.

I can't select a default agent queue and I can't queue my build or release. How do I fix this?

See queues.

I use Team Foundation Server on-premises and I don't see some of these features. Why not?

Some of these features are available only on Visual Studio Team Services and not yet available on-premises. Some features are available on-premises if you have upgraded to the latest version of TFS.