Skip to main content

Get App Registrations by User Principal Name

 

$AccessToken = "<yourazureadaccesstoken>"
$UserPrincipalName = "<userprincipalnametosearchfor>"

$Header = @{
    "Authorization" = "Bearer $($AccessToken)"
}

$Params = @{
    "Method"      = "Get"
    "Uri"         = "https://graph.microsoft.com/v1.0/applications"
    "Headers"     = $Header
    "ContentType" = "application/json"
}

$Result = Invoke-RestMethod @Params
$AllApplications = $Result.value
while ($Result.'@odata.nextLink') {
    $Result = Invoke-RestMethod -Uri $Result.'@odata.nextLink' -Headers $Header
    $AllApplications += $Result.value
}

$UsersApplication = @()

Foreach($Application in $AllApplications){
    $Params.Uri = "https://graph.microsoft.com/v1.0/applications/$($Application.id)/owners?`$select=id,userPrincipalName"
    $ApplicationInfo = (Invoke-RestMethod @Params).value
    if($ApplicationInfo.userPrincipalName -eq $UserPrincipalName){# -and $ApplicationInfo.userPrincipalName.count -eq 1){ #This can be displayed if you want to serach only for apps where the user is the only owner
        $UsersApplication += $Application
    }
}