Shelvesets/Get-ShelvesetsForBranch.ps1

function Get-OpenCodeReveiews
{
    Invoke-TFSAPI -Url (Get-UrlForOpenCodeReviewsQuery)
}

function Get-ShelvesetsForBranch
{
    Param(
        [Parameter(Mandatory=$true)]
        [string]$Branch
    )

    $Shelvesets = @()

    $CodeReviews = Get-OpenCodeReveiews
    foreach ($CodeReviewWorkItem in $CodeReviews.workItems)
    {
        $WorkItem = Invoke-TFSAPI -Url $CodeReviewWorkItem.url        
        $ShelvesetName = '{0};{1}' -f $WorkItem.fields.'Microsoft.VSTS.CodeReview.Context', $WorkItem.fields.'Microsoft.VSTS.CodeReview.ContextOwner'
        if (Get-ShelvesetHasChangesForBranch -Branch $Branch -Shelveset $ShelvesetName)
        {
            $Shelvesets += $ShelvesetName
        }
    }

    $Shelvesets
}

function Get-ShelvesetHasChangesForBranch
{
    Param(
        [Parameter(Mandatory=$true)]
        [string]$Branch,
        [Parameter(Mandatory=$true)]
        [string]$ShelvesetName
    )

    $Shelveset = Invoke-TFSAPI -Url "_apis/tfvc/shelvesets/$ShelvesetName"
    if ($Shelveset._links.changes.href -ne '')
    {
        $Changes = Invoke-TFSAPI -Url $Shelveset._links.changes.href
        foreach ($Change in $Changes.value)
        {
            if ($Change.item.path.IndexOf($Branch) -gt -1)
            {
                return $true
            }
        }
    }

    return $false
}

function Get-UrlForOpenCodeReviewsQuery
{
    '{0}345c3d59-5916-438a-a8a0-0d49a48c6168/_apis/wit/wiql/b348c81a-036f-41e5-a0d9-dde6330276b1' -f (Get-TFSCollectionURL)
}

Export-ModuleMember -Function Get-ShelvesetsForBranch