Export Azure Devops Users and group assignments into csv using az cli

%3CLINGO-SUB%20id%3D%22lingo-sub-2288323%22%20slang%3D%22en-US%22%3EExport%20Azure%20Devops%20Users%20and%20group%20assignments%20into%20csv%20using%20az%20cli%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2288323%22%20slang%3D%22en-US%22%3E%3CDIV%3E%3CDIV%3E%3CSPAN%3E%3CSPAN%3EBelow%20script%20exports%20all%20users%20and%20their%20respective%20group%20assignments%20in%20an%20azure%20devops%20organization%20into%20a%20csv%20file.%26nbsp%3B%3C%2FSPAN%3E%3C%2FSPAN%3E%3CDIV%3E%26nbsp%3B%3CDIV%3E%3CSPAN%3E%3CSPAN%3EScript%20needs%20PATKey%20and%20Organization%26nbsp%3B%3C%2FSPAN%3E%3C%2FSPAN%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%5Bstring%5D%24PATKey%26nbsp%3B%3D%26nbsp%3B%22Paste%26nbsp%3BPAT%26nbsp%3BKey%26nbsp%3BHere%22%26nbsp%3B%2C%0A%5Bstring%5D%24Organization%26nbsp%3B%3D%26nbsp%3B%22Your%26nbsp%3BOrganization%26nbsp%3BURL%22%0A%0A%24UserGroupsObject%26nbsp%3B%3D%26nbsp%3B%40()%0A%0A%24PATKey%26nbsp%3B%7C%26nbsp%3Baz%26nbsp%3Bdevops%26nbsp%3Blogin%26nbsp%3B--org%26nbsp%3B%24Organization%0A%0Aaz%26nbsp%3Bdevops%26nbsp%3Bconfigure%26nbsp%3B--defaults%26nbsp%3Borganization%3D%24Organization%0A%0A%24Users%26nbsp%3B%3D%26nbsp%3Baz%26nbsp%3Bdevops%26nbsp%3Buser%26nbsp%3Blist%26nbsp%3B--org%26nbsp%3B%24Organization%26nbsp%3B%7C%26nbsp%3BConvertFrom-Json%0A%0Aforeach%26nbsp%3B(%24user%26nbsp%3Bin%26nbsp%3B%24Users.members)%26nbsp%3B%7B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24activeUserGroups%26nbsp%3B%3D%26nbsp%3Baz%26nbsp%3Bdevops%26nbsp%3Bsecurity%26nbsp%3Bgroup%26nbsp%3Bmembership%26nbsp%3Blist%26nbsp%3B--id%26nbsp%3B%24user.user.principalName%26nbsp%3B--org%26nbsp%3B%24Organization%26nbsp%3B--relationship%26nbsp%3Bmemberof%26nbsp%3B%7C%26nbsp%3BConvertFrom-Json%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%5Barray%5D%24groups%26nbsp%3B%3D%26nbsp%3B(%24activeUserGroups%26nbsp%3B%7C%26nbsp%3BGet-Member%26nbsp%3B-MemberType%26nbsp%3BNoteProperty).Name%0A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bforeach%26nbsp%3B(%24group%26nbsp%3Bin%26nbsp%3B%24groups)%26nbsp%3B%7B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24UserGroupsObject%26nbsp%3B%2B%3D%26nbsp%3BNew-Object%26nbsp%3B-TypeName%26nbsp%3BPSObject%26nbsp%3B-Property%26nbsp%3B%40%7B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BprincipalName%26nbsp%3B%3D%26nbsp%3B%24user.user.principalName%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BdisplayName%26nbsp%3B%26nbsp%3B%26nbsp%3B%3D%26nbsp%3B%24user.user.displayName%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BGroupName%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3D%26nbsp%3B%24activeUserGroups.%24group.principalName%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D%0A%7D%0A%0A%24UserGroupsObject%26nbsp%3B%7C%26nbsp%3BExport-CSV%26nbsp%3B-Path%26nbsp%3B%22C%3A%5CDevopsUsersAndAssignments.csv%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2288323%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20CLI%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20DevOps%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
New Contributor
Below script exports all users and their respective group assignments in an azure devops organization into a csv file. 
 
Script needs PATKey and Organization 
 

 

 

[string]$PATKey = "Paste PAT Key Here" ,
[string]$Organization = "Your Organization URL"

$UserGroupsObject = @()

$PATKey | az devops login --org $Organization

az devops configure --defaults organization=$Organization

$Users = az devops user list --top 10000 --org $Organization | ConvertFrom-Json

foreach ($user in $Users.members) {
    $activeUserGroups = az devops security group membership list --id $user.user.principalName --org $Organization --relationship memberof | ConvertFrom-Json
    [array]$groups = ($activeUserGroups | Get-Member -MemberType NoteProperty).Name

    foreach ($group in $groups) {
        $UserGroupsObject += New-Object -TypeName PSObject -Property @{
            principalName = $user.user.principalName
            displayName   = $user.user.displayName
            GroupName     = $activeUserGroups.$group.principalName
        }
    }
}

$UserGroupsObject | Export-CSV -Path "C:\DevopsUsersAndAssignments.csv"

 

 

2 Replies
I was trying to export the users using this powershell script, but it is giving me only users who names start with "A" or Is there any limit on the output. I am getting only 168 users as output . Please help me with this, I want to export all users which are around 800.
Thanks @rishabhkanwalirk for reporting this issue.

az devops user list command is having a 'top' argument and I have now increased that to maximum value(10000). With this change upto 10000 users can be listed during execution.