The steps outlined below will walk you through the process of creating a batch file to handle stopping and starting the service as well as creating a scheduled task to call the batch file. In order to create the batch file the first thing you’ll need to do is launch Notepad. The basic command for calling a service stop and start are shown below.
-->Azure Pipelines | Azure DevOps Server 2019 | TFS 2018 | TFS 2017 | TFS 2015
Use this task in a build or release pipeline to run a program from the command prompt.
Note
In Microsoft Team Foundation Server (TFS) 2018 and previous versions,run 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
The CmdLine task also has a shortcut syntax in YAML:
![File File](https://www.visualstudio.com/en-us/news/releasenotes/media/tfs2018_89.png)
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 | Contents of the script you want to run |
Optional | |
Working directory | Specify the working directory in which you want to run the command. If you leave it empty, the working directory is $(Build.SourcesDirectory). |
Fail on standard error | If this is true , this task will fail if any errors are written to stderr . |
Environment variables | 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 like this: |
Control options |
Example
On the Build tab of a build pipeline, add these tasks:
Utility: Command Line | Get the date.
|
Utility: Command Line | Display the operating system version.
|
Utility: Command Line | Display the environment variables.
|
Utility: Command Line | Display all files in all the folders created by the build pipeline.
|
Open source
This task is open source on GitHub. Feedback and contributions are welcome.
Q & A
Where can I learn Windows commands?
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 onAzure Pipelinesand not yet available on-premises. Some features are available on-premises if you haveupgraded to the latest version of TFS.
On Windows 10, a batch file is a special kind of text file that typically has a .bat extension, which can include one or multiple commands that Command Prompt can understand and run in sequence to perform various actions.
Usually, you can input commands manually to perform a particular task, but a batch file simplifies the work of retyping commands, saving you time.
Also, even though, there are other tools, such as PowerShell, that write more advanced scripts, using batch files with Command Prompt is a convenient option when you need to run commands to change system settings, automate routines, and launch apps or websites.
In this Windows 10 guide, we walk you through the steps to get started creating and running your first batch file on your computer.
How to create a batch file on Windows 10
The process of writing a batch (script or batch script) file is easy. You only need a text editor and some knowledge using Command Prompt. In the steps below, we detail the steps for creating a simple and advanced batch file, as well as the steps to write a script to change the system settings.
Creating simple batch file
To create a simple batch file on Windows 10, use these steps:
- Open Start.
- Search for Notepad, and click the top result to launch the app.
- Type the following lines to create a simple batch file:Source: Windows CentralThe above script outputs the phrase, 'Congratulations! Your first batch file executed successfully' on the screen.Source: Windows Central
- @ECHO OFF -- Turns off the display prompt to show only the message on a clean line. Usually, this particular line goes at the beginning of the file. (You can use this command without '@,' but the symbol hides the command being executed for a cleaner output.)
- ECHO -- Prints the desired text on the screen.
- PAUSE -- Prevents the console window from closing after executing the command. You can use this command at the end of the script or after a specific command when running multiple tasks, and you want to pause between them.
- Click the File menu.
- Select the Save as option.
- Type a name for the script — for example, first_simple_batch.bat.Quick note: While batch files typically use the .bat file extensions, you may also see scripts using the .cmd or .btm file extensions.
Once you complete the steps, you can double-click the file to run it, or you can use the steps below to learn the different ways to execute a batch file on Windows 10.
Creating advanced batch file
To create an interactive batch file that executes multiple commands, use these steps:
- Open Start.
- Search for Notepad, and click the top result to launch the app.
- Type the following lines to create a complex batch file:Source: Windows CentralThe above batch script will run a series of commands to query different system information that is grouped into three different categories, including OS INFO, HARDWARE INFO, and NETWORK INFO.Source: Windows Central
- @ECHO OFF -- Turns off the display prompt to show only the message on a clean line. Usually, this particular line goes at the beginning of the file. (You can use this command without '@,' but the symbol hides the command being executed for a cleaner output.)
- TITLE -- Displays a custom name in the title bar of the console.
- :: -- Allows you to add comments and documentation information, which are ignored when the batch file runs.
- ECHO -- Prints the text on the screen.
- PAUSE -- Prevents the console window from closing after executing the command. You can use this command at the end of the script or after a specific command when running multiple tasks, and you want to pause between them.
- Click the File menu.
- Select the Save as option.
- Type a name for the script — for example, first_advanced_batch.bat.
After you complete these steps, you can run the script double-clicking the .bat file, or you can use the steps below to learn the different ways to execute a batch on Windows 10.
Creating actionable batch file
You're not limited to showing information on a Windows 10 console. You can also write non-interactive batch files to perform virtually any task. For instance, to write a batch file that runs a specific command without the need for user interaction, use these steps:
- Open Start.
- Search for Notepad, and click the top result to launch the app.
- Copy and paste the following command:Source: Windows CentralThe above command is just an ordinary command that maps a network folder as a drive on File Explorer using the 'Z' drive letter.Source: Windows Central
- Click the File menu.
- Select the Save as option.
- Type a name for the script — for example, map-z-drive.bat.
Once you complete the steps, when you run the batch file, the command will map a shared network folder with the settings, you specified without the need to open Command Prompt. Although we only specified one command in the file, you can include as many commands as you like, as long as you write one per line.
How to run a batch file on Windows 10
On Windows 10, there are a least three ways to run a batch file. You can run a batch on-demand (using File Explorer or Command Prompt). You can create a scheduled task using Task Scheduler. Or you can place the script in the 'Startup' folder to run it every time you sign in to your computer.
Running batch file on-demand
When you need to run a batch file on-demand, you can use File Explorer or Command Prompt.
File Explorer
To run a batch file using File Explorer, use these steps:
- Open File Explorer.
- Navigate to the folder with the script.
- Double-click the batch file to run it.Source: Windows CentralIf you're executing a command that requires administrator privileges, you'll need to run the script as an admin by right-clicking the batch file and selecting the Run as administrator option, and clicking the Yes button.
After you complete the steps, the batch will run each command in sequence displaying the results on the screen.
Command Prompt
To run a batch file from Command Prompt, use these steps.
- Open Start.
- Search for Command Prompt, right-click the top result, and select the Run as administrator option.
- Type the path and the name of the batch file and press Enter:
C:PATHTOFOLDERBATCH-NAME.bat
For example, the following command runs the batch file located in the user's 'batch' folder inside the 'Downloads' folder:C:UsersuserDownloadsbatchfirst_simple_batch.bat
Source: Windows Central
Once you complete the steps, the output will display on the screen regardless of the script containing the 'PAUSE' command or not.
Running batch file on scheduled
To schedule a batch file on Windows 10, you'll need to use the Task Scheduler with these steps:
- Open Start.
- Search for Task Scheduler, and click the top result to open the app.
- Right-click the 'Task Scheduler Library' branch and select the New Folder option.
- Type a name for the folder — for example, MyScripts.Quick note: It's not necessary to create a folder, but it's recommended to organize your tasks.
- Click the OK button.
- Expand the 'Task Scheduler Library' branch.
- Right-click the MyScripts folder.
- Select the Create Basic Task option.Source: Windows Central
- In the 'Name' field, type a short descriptive name for the task — for example, SystemInfoBatch.Source: Windows Central
- (Optional) In the 'Description' field, create a description for the task.
- Click the Next button.
- Select the Monthly option.Source: Windows CentralTask Scheduler allows you to select from a number of triggers, including on a specific date, during startup, or when you or a particular user signs in to the computer. Depending on your requirements, you'll need to configure additional parameters. In this case, we're selecting the option to run a task every month.
- Click the Next button.
- Using the 'Start' settings, specify the day and time when the task should start running.
- Use the 'Monthly' drop-down menu to pick the months of the year that you want to run the task.Source: Windows Central
- Use the 'Days' or 'On' drop-down menu to specify the days that the task will run.Source: Windows Central
- Click the Next button.
- Select the Start a program option to run the batch file.Source: Windows Central
- In the 'Program/script' field, specify the path for the batch file.Source: Windows Central
- Click the Finish button.
Once you complete the steps, the task will be saved, and it'll run the batch file on the schedule you specified.
These instructions cover the steps to create a basic task. If you want to create a more customizable task, use this guide.
Running batch files on startup
Alternatively, if you want to run a batch file every time that you sign in to your device, use these easy steps:
- Open File Explorer.
- Navigate to the folder that contains the batch file.
- Right-click the file and select the Copy option.
- Use the Windows key + R keyboard shortcut to open the Run command
- Type the following command, and click the OK button:
shell:startup
Source: Windows Central - Click the Paste option from the 'Home' tab in the Startup folder. (Or you can click the Paste shortcut button to create a shortcut to the batch file.)Source: Windows Central
- Sign out of your Windows 10 account.
- Sign back into your account.
After you complete the steps, every time you sign in to your account, the batch file will automatically execute in sequence the commands you wrote.
We're focusing this guide on Windows 10, but the ability to use batch files has been around for many years, which means that you can refer to these instructions if you're still using Windows 8.1 or Windows 7.
More Windows 10 resources
For more helpful articles, coverage, and answers to common questions about Windows 10, visit the following resources:
We may earn a commission for purchases using our links. Learn more.
breaking barriersMicrosoft's latest ad features the first woman to ever coach the Super Bowl
Microsoft's Super Bowl LIV ad premiered online today. It focuses on Katie Sowers, an offensive assistant coach for the San Franciso 49ers, who is the first woman to ever coach in a Super Bowl.