Skip to content

Instantly share code, notes, and snippets.

@cschar
Created April 24, 2026 08:22
Show Gist options
  • Select an option

  • Save cschar/83b221f6e83f33477a037c5b53b40fae to your computer and use it in GitHub Desktop.

Select an option

Save cschar/83b221f6e83f33477a037c5b53b40fae to your computer and use it in GitHub Desktop.
a reset
param(
[Parameter(Mandatory=$true)]
[string]$TenantId,
[Parameter(Mandatory=$true)]
[string]$ClientId,
[Parameter(Mandatory=$true)]
[string]$ClientSecret,
[Parameter(Mandatory=$true)]
[string]$TargetApplicationName,
[string]$OwnerUserPrincipalName = "firstname.lastname@gmail.com"
)
Install-Module Microsoft.Graph -Scope CurrentUser -Force
$secureSecret = ConvertTo-SecureString $ClientSecret -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($ClientId, $secureSecret)
Connect-MgGraph -TenantId $TenantId -ClientSecretCredential $credential -NoWelcome
$app = Get-MgApplication -Filter "displayName eq '$TargetApplicationName'"
if (-not $app) {
throw "No application found with displayName '$TargetApplicationName'"
}
if ($app.Count -gt 1) {
throw "Multiple applications found with displayName '$TargetApplicationName'. Use AppId/ObjectId instead."
}
$user = Get-MgUser -UserId $OwnerUserPrincipalName
$ownerRef = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/directoryObjects/$($user.Id)"
}
New-MgApplicationOwnerByRef `
-ApplicationId $app.Id `
-BodyParameter $ownerRef
Write-Host "Added $OwnerUserPrincipalName as owner of application '$TargetApplicationName'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment