Command Line task

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

Use this task to run a program from the command prompt.

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.

Demands

None

YAML snippet

# Command line
# Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
- task: CmdLine@2
  inputs:
    script: 'echo Write your commands here.' 
    #workingDirectory: # Optional
    #failOnStderr: false # Optional

The CmdLine task also has a shortcut syntax in YAML:

- script: # script path or inline
  workingDirectory: #
  displayName: #
  failOnStderr: #
  env: { string: string } # mapping of environment variables to add

Running batch and .CMD files

Azure Pipelines puts your inline script contents into a temporary batch file (.cmd) in order to run it. When you want to run a batch file from another batch file in Windows CMD, you must use the call command, otherwise the first batch file is terminated. This will result in Azure Pipelines running your intended script up until the first batch file, then running the batch file, then ending the step. Additional lines in the first script wouldn't be run. You should always prepend call before executing a batch file in an Azure Pipelines script step.

Important

You may not realize you're running a batch file. For example, npm on Windows, along with any tools that you install using npm install -g, are actually batch files. Always use call npm <command> to run NPM commands in a Command Line task on Windows.

Arguments

Argument Description
script
Script
(Required) Contents of the script you want to run
Default value: echo Write your commands here\n\necho Hello world\n"
workingDirectory
Working directory
(Optional) Specify the working directory in which you want to run the command. If you leave it empty, the working directory is $(Build.SourcesDirectory).
failOnStderr
Fail on Standard Error
If this is true, this task will fail if any errors are written to stderr
env
Environment variables
(Optional) A list of additional items to map into the process's environment.
For example, secret variables are not automatically mapped. If you have a secret variable called Foo, you can map it in as shown in the following example.
- script: echo %MYSECRET%
  env:
    MySecret: $(Foo)

Example

steps:
- script: date /t
  displayName: Get the date
- script: dir
  workingDirectory: $(Agent.BuildDirectory)
  displayName: List contents of a folder
- script: |
    set MYVAR=foo
    set
  displayName: Set a variable and then display all
  env:
    aVarFromYaml: someValue

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

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.