Skip to content

Instantly share code, notes, and snippets.

@joerodgers
Last active August 6, 2026 17:26
Show Gist options
  • Select an option

  • Save joerodgers/1552c1cc130c4648fce8f57b1b13efaa to your computer and use it in GitHub Desktop.

Select an option

Save joerodgers/1552c1cc130c4648fce8f57b1b13efaa to your computer and use it in GitHub Desktop.
Uses a resource query to pull back all Copilot Studio and Agent Builder agents in the tenant.
#requires -modules "Microsoft.PowerApps.Administration.PowerShell"
function Get-AgentInventory
{
[CmdletBinding()]
param
(
)
begin
{
$accessToken = Get-JwtToken -Audience "https://api.powerplatform.com/"
$headers = @{ "Authorization" = "Bearer $accessToken"; "Content-Type" = "application/json"; "Accept" = "application/json" }
$skipToken = ""
$skip = 0
$top = 1000
$template = @'
{{
"Options": {{
"Top": {0},
"Skip": {1},
"SkipToken": "{2}"
}},
"TableName": "PowerPlatformResources",
"Clauses": [
{{
"$type": "where",
"FieldName": "type",
"Operator": "in~",
"Values": [
"'microsoft.copilotstudio/agents'"
]
}},
{{
"$type": "extend",
"FieldName": "joinKey",
"Expression": "tolower(tostring(properties.environmentId))"
}},
{{
"$type": "project",
"FieldList": [
"joinKey",
"name",
"type",
"properties"
]
}},
{{
"$type": "join",
"JoinKind": "leftouter",
"RightTable": {{
"TableName": "PowerPlatformResources",
"Clauses": [
{{
"$type": "where",
"FieldName": "type",
"Operator": "==",
"Values": [
"'microsoft.powerplatform/environments'"
]
}},
{{
"$type": "project",
"FieldList": [
"joinKey = tolower(name)",
"environmentName = properties.displayName",
"environmentRegion = location",
"environmentType = properties.environmentType",
"isManagedEnvironment = properties.isManaged",
"environmentGroupId = tolower(properties.environmentGroupId)"
]
}}
]
}},
"LeftColumnName": "joinKey",
"RightColumnName": "joinKey"
}},
{{
"$type": "join",
"JoinKind": "leftouter",
"RightTable": {{
"TableName": "PowerPlatformResources",
"Clauses": [
{{
"$type": "where",
"FieldName": "type",
"Operator": "==",
"Values": [
"'microsoft.powerplatform/environmentgroups'"
]
}},
{{
"$type": "project",
"FieldList": [
"environmentGroupId = tolower(name)",
"environmentGroupName = properties.displayName"
]
}}
]
}},
"LeftColumnName": "environmentGroupId",
"RightColumnName": "environmentGroupId"
}},
{{
"$type": "orderby",
"FieldNamesAscDesc": {{
"tostring(properties.createdAt)": "desc"
}}
}}
]
}}
'@
}
process
{
$uri = "https://api.powerplatform.com/resourcequery/resources/query?api-version=2022-03-01-preview"
$agents = do
{
$body = $template -f $top, $skip, $skipToken
$response = Invoke-RestMethod -Method Post -Uri $uri -Body $body -Headers $headers
$skipToken = $response.skipToken
$skip += $top
$response.data
}
while( $skipToken )
foreach( $agent in $agents )
{
[PSCustomObject] @{
displayName = $agent.properties.displayName
name = $agent.name
entraAgentId = $agent.properties.entraAgentId
entraAgentBlueprintId = $agent.properties.entraAgentBlueprintId
environmentId = $agent.properties.environmentId
environmentName = $agent.environmentName
environmentRegion = $agent.environmentRegion
environmentType = $agent.environmentType
environmentIsManaged = $agent.isManagedEnvironment
environmentGroupId = $agent.environmentGroupId
environmentGroupName = $agent.environmentGroupName
createdAt = $agent.properties.createdAt
createdBy = $agent.properties.createdBy
sharedWithViewersGroupCount = $agent.properties.sharedWithViewers.groupCount
sharedWithViewersUserCount = $agent.properties.sharedWithViewers.userCount
sharedWithViewersEntireTenant = $agent.properties.sharedWithViewers.entireTenant
authentication = $agent.properties.authentication
orchestration = $agent.properties.orchestration
ownerId = $agent.properties.ownerId
isQuarantined = $agent.properties.isQuarantined
isCLIAgent = $agent.properties.isCLIAgent
lastPublishedAt = $agent.properties.lastPublishedAt
schemaName = $agent.properties.schemaName
sharedWithEditorssGroupCount = $agent.properties.sharedWithEditors.groupCount
sharedWithEditorsUserCount = $agent.properties.sharedWithEditors.userCount
createdIn = $agent.properties.createdIn
isManaged = $agent.properties.isManaged
channels = $agent.properties.channels -join ","
model = $agent.properties.model
entraAppId = $agent.properties.entraAppId
}
}
}
end
{
}
}
$null = Get-JwtToken -Audience "https://api.powerplatform.com/"
Get-AgentInventory | Export-Csv -Path "C:\_temp\agent-inventory_resource-query.csv" -NoTypeInformation
<# Example output
displayName : Test Agent
environmentId : e1887b3f-8b6f-ef25-905a-b3d01a1d590e
environmentName : William Beringer
environmentRegion : unitedstates
environmentType : Developer
environmentIsManaged : True
environmentGroupId :
environmentGroupName :
createdAt : 2026-05-05T15:14:40Z
createdBy : a8734c7e-a19b-41c6-b384-5e550ca02b9e
sharedWithViewersGroupCount : 0
sharedWithViewersUserCount : 0
sharedWithViewersEntireTenant : False
authentication : Microsoft Entra
orchestration : Generative
ownerId : a8734c7e-a19b-41c6-b384-5e550ca02b9e
isQuarantined : False
isCLIAgent : False
lastPublishedAt : 2026-05-05T15:15:53Z
schemaName : new_TestAgent
sharedWithEditorssGroupCount : 0
sharedWithEditorsUserCount : 0
createdIn : Copilot Studio
isManaged : False
channels : Teams,Microsoft 365 Copilot,Direct Line Channels
model : Claude Sonnet 4.6
entraAppId :
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment