How to get the object ID as a variable

honeybee170 181 Reputation points
2023-11-07T13:39:35.1733333+00:00

Hello,

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:

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.
22,111 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 111.3K Reputation points MVP
    2023-11-07T14:14:00.01+00:00

    Hi @honeybee170 ,

    please try this:

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

    Another option you can try:

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

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

    Regards

    Andreas Baumgarten

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Dillon Silzer 57,431 Reputation points
    2023-11-07T16:13:00.37+00:00

    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
    2023-11-08T06:59:55.0466667+00:00

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

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.