Forum Discussion
RaviKiranS
May 04, 2023Copper Contributor
How to add a comment with rich text (text+images) to the work item (task) with Powershell
Hi All,
I am trying to add a comment into discussion and other custom fields based of rich text programmatically (using PowerShell). Can you please guide me how to upload the image. I browsed onlin...
Ankido88
Sep 14, 2025Copper Contributor
Hi
prerequiaties token (PAT) win work items read and write and project and teams read scops.
API v 7.1
# Configuration
$organization = "your-org" # e.g., "contoso"
$project = "your-project" # e.g., "Fabrikam-Fiber-Git"
$workItemId = 123 # Work item ID
$pat = "your-pat" # Personal Access Token
$imagePath = "C:\path\to\image.png" # Path to image
# Base64 encode PAT for auth
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat"))
# Step 1: Upload image as attachment
$uploadUrl = "https://dev.azure.com/$organization/$project/_apis/wit/attachments?fileName=$(Split-Path $imagePath -Leaf)&api-version=7.1"
$imageBytes = [System.IO.File]::ReadAllBytes($imagePath)
$headers = @{
Authorization = "Basic $base64AuthInfo"
'Content-Type' = 'application/octet-stream'
}
try {
$uploadResponse = Invoke-RestMethod -Uri $uploadUrl -Method Post -Body $imageBytes -Headers $headers
$attachmentUrl = $uploadResponse.url
Write-Host "Image uploaded: $attachmentUrl"
} catch {
Write-Error "Upload failed: $($_.Exception.Message)"
exit
}
# Step 2: Create rich text with image (Markdown)
$text = "Issue description. See image: "
# Step 3: Add comment to work item
$commentUrl = "https://dev.azure.com/$organization/$project/_apis/wit/workItems/$workItemId/comments?api-version=7.1-preview.3"
$body = @{ text = $text } | ConvertTo-Json
$headers['Content-Type'] = 'application/json'
try {
$commentResponse = Invoke-RestMethod -Uri $commentUrl -Method Post -Body $body -Headers $headers
Write-Host "Comment added: $($commentResponse.commentId)"
} catch {
Write-Error "Comment failed: $($_.Exception.Message)"
}