How to get the object ID as a variable

honeybee170 181 Reputation points


with this command I can get the object ID of a particular user:

Get-AzureADUser -ObjectID "$username" | Select-Object ObjectId

If I try to get it as a variable like:

$object_id = Get-AzureADUser -ObjectID "$username" | Select-Object ObjectId
Write-Host $object_id

I get a weird output which starts with an '@' - see screenshot:


How can I get the value as a variable? Thank you for your answer in advance.

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
17,567 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 77,981 Reputation points MVP

    Hi @honeybee170 ,

    please try this:

    $object_id = (Get-AzureADUser -ObjectID "username").ObjectID

    Another option you can try:

    $object_id = Get-AzureADUser -ObjectID "username" | Select-Object -ExpandProperty ObjectID 

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)


    Andreas Baumgarten

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Dillon Silzer 49,501 Reputation points

    Hello honeybee,

    When you were using Write-Host and receiving a @{} this means it is an array. Within the array you can call objects (if it has multiple objects inside). The way you can do this is to use:

    Write-Host $object_id.ObjectId

    See the example output:

    User's image

    If this is helpful please accept answer.

    0 comments No comments

  2. honeybee170 181 Reputation points

    Thank you very much for your answers! It fixed my issue! :)

    0 comments No comments