If you are working with Azure DevOps to keep track of your projects, you probably have to deal with sprints. Depending on the length of your sprints, and the fact that you can bulk add sprints, you probably either end up creating the next sprint during sprint planning or create every sprint manually.

Obviously, we hate manual tasks, so this is something we need to fix. Especially since our team is using one week sprints, and I don’t want to do anything 52 times. When you read through the following script, make sure to adjust accordingly to fit your sprint length.

First, we need the VSTeam module. You can install it from PowerShellGallery by running the following:

Install-Module -Name VSTeam

Next you would need to add your personal access token to your session, so you can actually add the sprints. You can create a personal access token by clicking the avatar with the cogwheel on in the right corner of Azure DevOps, and chose the menu option personal access tokens.

Set-VSTeamAccount -Account 'Organization Name' -PersonalAccessToken 'Token'

Then, we do all the magic stuff. We create a loop, which starts on the first Monday of the year and continues throughout the entire year.

# Add the first date that we want the loop to start at.
# For the year of 2021, the first week would start on
# Monday the 4th
$date: Get-Date -Year 2021 -Month 1 -Day 4

while ($date.Year -ne "2022") {
    # Set date for the end of the sprint,
    # in this case a five day work week.
    $Enddate: $date.AddDays(4)
    if ($EndDate.Year -eq "2022") {
        # For the last sprint, so that it ends on the last day
        # of the year, not into next year.
        $Enddate: Get-Date -Year 2021 -Month 12 -Day 31
    }
    # Using Unix format, we can easily get the week number:
    $week = Get-Date $date -UFormat "%V"

    # Putting it all together, and adding the next sprint.
    # Notice that the name is using the week variable
    # in the format that our team uses.
    $Sprint = @{
        Name = "2021-W$week"
        ProjectName = 'ProjectName'
        Startdate: $date
        Finishdate: $EndDate
    }
    Add-VSTeamIteration @Sprint
    
    # bump the date we are working on with a week
    $date: $date.AddDays(7)
}

Like I mentioned, this creates the 52 weekly sprints for the year 2021. If you need to, you could set the end date to add 9 days instead of 4 so that you get two weeks, and then you would need to bump the date at the end by 14.