Batch Script task

Azure Pipelines | Azure DevOps Server 2020 | Azure DevOps Server 2019 | TFS 2018 - TFS 2015

Use this task to run a Windows .bat or .cmd script. Optionally, allow it to permanently modify environment variables.

Note

This task is not compatible with Windows containers. If you need to run a batch script on a Windows container, use the command line task instead.

For information on supporting multiple platforms, see cross platform scripting.

Note

In Microsoft Team Foundation Server (TFS) 2018 and previous versions, build and release pipelines are called definitions, runs are called builds, service connections are called service endpoints, stages are called environments, and jobs are called phases.

YAML snippet

# Batch script
# Run a Windows command or batch script and optionally allow it to change the environment
- task: BatchScript@1
  inputs:
    filename: 
    #arguments: # Optional
    #modifyEnvironment: False # Optional
    #workingFolder: # Optional
    #failOnStandardError: false # Optional

Arguments

Argument Description
filename
Path
(Required) Path of the cmd or bat script to execute. Should be fully qualified path or relative to the default working directory
arguments
Arguments
(Optional) Specify arguments to pass to the script.
modifyEnvironment
Modify environment
(Optional) Determines whether environment variable modifications will affect subsequent tasks
Default value: False
workingFolder
Working folder
(Optional) Current working directory when script is run. Defaults to the agent's default working directory
failOnStandardError
Fail on Standard Error
(Optional) If this is true, this task will fail if any errors are written to the StandardError stream.
Default value: false

Example

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:
@dir %AGENT_WORKFOLDER%
echo AGENT_BUILDDIRECTORY contents:
@dir %AGENT_BUILDDIRECTORY%
echo BUILD_SOURCESDIRECTORY contents:
@dir %BUILD_SOURCESDIRECTORY%
echo Over and out.

On the Build tab of a build pipeline, add this task:


Utility: Batch Script

Run test.bat.

  • Path: test.bat

Open source

This task is open source on GitHub. Feedback and contributions are welcome.

FAQ

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. Variables are available in expressions as well as scripts; see variables to learn more about how to use them. There are some predefined build and release variables you can also rely on.

Do I need an agent?

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

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

See Troubleshoot Build and Release.

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

See Agent pools.

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

Some of these features are available only on Azure Pipelines and not yet available on-premises. Some features are available on-premises if you have upgraded to the latest version of TFS.