aboutBlog

Learn DevOps Step-by-Step Tutorials and fixing related issues.

Welcome to Py-Bucket, your go-to blog for DevOps tutorials and production issues fixes guide.

  • ✔ Beginner-friendly DevOps guides
  • ✔ Real-world production issues and fixes

Create Azure DevOps Custom Task Using PowerShell (Step-by-Step Guide)

Create Azure DevOps Custom Task Using PowerShell (Step-by-Step Guide)
Azure DevOps custom task creation using PowerShell step by step

What Are Azure DevOps Extensions?

Extensions are simple add-ons that can be customize and extend  your Devops experience.
Extensions provide new capabilities when they are installed in the Azure devops orgnization. 

Prerequisites for Creating Azure DevOps Custom Task:

1. Azure DevOps Orgnization
2. Code editor (suggested to have visual studio code)
3. Node.js
4. Azure DevOps CLI 
5. Azure DevOps extension SDK 


Install Required Tools (Node.js, Azure DevOps CLI, SDK)

Here i am using mac os,  (please follow steps for your OS)

1 . Node js 
Run below command to install Node js

brew install node

2. Azure Devops CLI
Run below command to install CLI

npm install -g tfx-cli

3. Azure devops extension SDK 

Run below command to install CLI

npm install azure-devops-extension-sdk --save


Folder structure

Follow below folder structure

|--- README.md    
|--- images                        
    |--- extension-icon.png  
|--- buildandreleasetask            // Task scripts location
    |--- task.json                  // Task definition
    |--- .ts (or) .ps1                   // Main task logic (as per code typescript or powershell)
    |--- package.json               // Node.js dependencies
    |--- tests/                     // Unit tests
        |--- 
|--- vss-extension.json             // Extension manifest


How to Create a Custom Azure DevOps Task Using PowerShell

Here we start the creation of custom task, all files and command i am giving below should be run in the buildandreleasetask folder.

1. Lets initialize the node js project with the help of npm command below:
npm init --yes

This will create the package.json file in the folder, this file lists the node js dependensies.

2. Install azure pipeline task lib:

npm install azure-pipelines-task-lib --save 

3. Create task.json file in  buildandreleasetask, initially you can take the content from the link later change the parameters as per your requirement.

note: make sure you updated data as per requirement and existing info. Update properties "id", "name", "friendlyname", "description" and "author"

task.json describes the indivisual task and what it executes. This includes the parameters that user need to provide and which script to execute.

The task.json configures that what user going to see when they select the task in the UI.

Sample task.json file, with types of input code: 

{
"$schema": "https://raw.githubusercontent.com/Microsoft/azure-pipelines-task-lib/master/tasks.schema.json",
"id": "{{taskguid}}",
"name": "{{taskname}}",
"friendlyName": "{{taskfriendlyname}}",
"description": "{{taskdescription}}",
"helpMarkDown": "",
"category": "Utility",
"author": "prashant",
"version": {
"Major": 0,
"Minor": 1,
"Patch": 0
},
"instanceNameFormat": "Echo $(samplestring)",
"inputs": [
{
"name": "samplestring",
"type": "string",
"label": "Sample String",
"defaultValue": "",
"required": true,
"helpMarkDown": "A sample string (content in i button)"
},
{
"name": "sampleboolean",
"type": "boolean",
"label": "Sample Boolean",
"defaultValue": false,
"required": true,
"helpMarkDown": "A sample boolean (content in i button)"
},
{
"name": "samplespick",
"type": "pickList",
"label": "Sample pickList",
"defaultValue": "test1",
"required": true,
"helpMarkDown": "A sample pickList (content in i button)",
"options": {
"test1": "test1",
"test2": "test2"
}
},
{
"name": "sampleradio",
"type": "radio",
"label": "Sample radio",
"defaultValue": false,
"required": true,
"helpMarkDown": "A sample radio (content in i button)",
"options": {
"test1": "test",
"test" : "some other info needed"
},
"visibleRule": "sampleboolean = true"
}
],
"execution": {
"PowerShell": {
"target": "test.ps1"
},
"workingDirectory": "$(currentDirectory)"
}
}


4. Install vstsTasksdk:

Create folder ps_modules i the buildandreleasetask and open powershell in the ps_module folder. Run below command in the folder using powershell

Save-Module -name VstsTaskSdk -Path .

in case of any version inside the ps_module folder, take the data ouside of folder and past it in ps_module folder and remove the version folder, else you will run into issues when it tries to find the references.

5. Powershell Script

Write the script where functionality implemented. (no need to write complete logic, as we creating initial draft)

for example:

[CmdletBinding()]
param()
Trace-VstsEnteringInvocation $MyInvocation

try {
# Get inputs
$Command = Get-VstsInput -samplestring "samplestring" -required
$IsEnabled = Get-VstsInput -sampleboolean "sampleboolean" -required
$PickListValue = Get-VstsInput -samplespick "samplespick" -required
$RadioValue = Get-VstsInput -sampleradio "sampleradio"

Write-Host "Command: $Command"
Write-Host "Is Enabled: $IsEnabled"
Write-Host "Pick List Value: $PickListValue"
Write-Host "Radio Value: $RadioValue"
}

finally {
Trace-VstsExitingInvocation $MyInvocation
}

How to Package and Publish Azure DevOps Extension

Once you have written code (complete mvp of extension) the next step is getting into market place. For this you need to pack all the files in a package. All extesnsions are packed in VSIX 2.0 compatible. tfx-cli provide the functionality to pack the files together. 
for this we need configuration file for pacakage which is nothing but the vss-extension.json  

On extension root directory, create file vss-extension.json  take sample file from here : link
or you can take below and make changes as per your requirement
{
"manifestVersion": 1,
"id": "testextension-task",
"name": "My Custom Tasks",
"version": "1.0.0",
"publisher": "your-publisher-id",
"targets": [
{
"id": "Microsoft.VisualStudio.Services"
}
],
"description": "Custom build and release tasks for Azure DevOps",
"categories": [
"Azure Pipelines"
],
"icons": {
"default": "images/pathto.png"
},
"files": [
{
"path": "buildandreleasetask"
}
],
"contributions": [
{
"id": "testextension",
"type": "ms.vss-distributed-task.task",
"targets": [
"ms.vss-distributed-task.tasks"
],
"properties": {
"name": "buildandreleasetask"
}
}
]
} 


Once the file is ready run the below command in the extension root folder:
tfx extension create --manifest-globs vss-extension.json

note: make sure before running the command you have updated task.json file also make sure you have provided correct path in json files.


FAQs


What is a custom task in Azure DevOps?

A custom task allows you to extend Azure DevOps pipelines by adding your own logic using PowerShell or Node.js.

How do I create a PowerShell task in Azure DevOps?

You can create a PowerShell task by defining a task.json file and writing a PowerShell script, then packaging it as an extension.

What is task.json in Azure DevOps?

task.json defines the configuration, inputs, and execution details of a custom task.

 

Featured posts

🔥 Featured Tutorials

Devops

DevOps Tutorials

Author Details

Hi, I'm Prashant — a full-time software engineer with a passion for automation, DevOps, and sharing what I learn. I started Py-Bucket to document my journey through tools like Docker, Kubernetes, Azure DevOps, and PowerShell scripting — and to help others navigate the same path. When I’m not coding or writing, I’m experimenting with side projects, exploring productivity hacks, or learning how to build passive income streams online. This blog is my sandbox — and you're welcome to explore it with me. Get in touch or follow me for future updates!