Create TFS work items by Powershell
#Use of client servers
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.TeamFoundation.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.TeamFoundation.WorkItemTracking.Client")
[psobject] $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer(“https://server/tfs/defaultCollection")
$WorkItemStore=$tfs.TfsTeamProjectCollection.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])
$project=$WorkItemStore.Projects["ProjectName"]
#Get work item by Id - if doesn't exist, throws exception
$WorkItemStore.GetWorkItem(1332)
#Create Task
$type=$project.WorkItemTypes["Task"]
$item = new-object Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem $type
$item.Title = “External Video: Dummy Task by Powershell"
$item.AreaPath = “SomeArea\12. External Video"
$item.IterationPath = “SomeArea\Release 1\Sprint 12"
$item.Description = "This task was created by Powershell as test to improve sprint planning with repeating tasks."
$item.save()
#Delete work item by id (only if you have permission)
Set-Alias -Name witadmin -Value 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\witadmin.exe'
#In general:
#witadmin destroywi /collection:[project collection URL] /id:[work item ID]
#Concrete example:
witadmin destroywi /collection:https://server/tfs/defaultCollection" /id:1363 /noprompt
#View item links - work item links
$WorkItemStore.GetWorkItem(1332).WorkItemLinks
#General links - to change sets etc.
$WorkItemStore.GetWorkItem(1332).Links
#Link work item - set item 391 as parent for 1363
$linkType = $WorkItemStore.WorkItemLinkTypes[[Microsoft.TeamFoundation.WorkItemTracking.Client.CoreLinkTypeReferenceNames]::Hierarchy]
$link = new-object Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemLink($linkType.ReverseEnd, 391) #391 is id of parent
$workItem=$WorkItemStore.GetWorkItem(1363)
$workItem.WorkItemLinks.Add($link)
$workItem.Save()
#myScript - create Task as child of other work item and fill values.
#input parameters
$parentId=391
$areaPath=“SomeArea\12. External Video"
$iterationPath=“SomeArea\Release 1\Sprint 12"
$defaultPriority=1000
#takes component name from areaPath (e. g. External Video)
$prefix=($areaPath.Split('\')[1]).split('.')[1].Trim()
#load assemblies
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.TeamFoundation.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.TeamFoundation.WorkItemTracking.Client")
#get TFS structure, project
[psobject] $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer(“https://server/tfs/defaultCollection")
$WorkItemStore=$tfs.TfsTeamProjectCollection.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])
$project=$WorkItemStore.Projects["SomeArea"]
$type=$project.WorkItemTypes["Task"]
#create parent-child relationship
$linkType = $WorkItemStore.WorkItemLinkTypes[[Microsoft.TeamFoundation.WorkItemTracking.Client.CoreLinkTypeReferenceNames]::Hierarchy]
$link = new-object Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemLink($linkType.ReverseEnd, $parentId) #391 is id of parent
#create Task
$item = new-object Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem $type
#set properties
#properties from common file - different for each item
$item.Title = [string]::format(“{0}: Dummy Task 3 by Powershell", $prefix)
$item.Description = "Test of writing backlog priority and Remaining Work."
#fill custom fields
$item.Fields["Backlog Priority"].Value = $defaultPriority
$item.Fields["Remaining Work"].Value = 4
#properties from input parameters - common for all items
$item.AreaPath = $areaPath
$item.IterationPath = $iterationPath
$item.WorkItemLinks.Add($link)
#save task
$item.Save()