skip to Main Content

I have a .TXT of unique DisplayNames (a total of 9). I’m simply populating an array and looping Get-MgUser through it. It returns all each twice.

#Display names list path
    $DnPath = "C:dirnamefilname.txt"
    
#Display names array
    $DnArr = get-content -path $DnPath
    
        
$DnArr | Foreach-Object { 
    Get-Mguser -search displayname:$_ -ConsistencyLevel eventual -Property id | Select-Object -Property id
    
    }

This results in (every ID is twice)

0eaba174-fab6-4cda-bf09-92391d586d48
15fb3a43-d8a1-4348-8e31-8f5814a38a69
1f08022d-85ba-40ba-9b79-c201c8f477b0
30792149-52da-44ec-9709-1911244bb183
6a4290f2-6397-4d58-9d20-e0b46a7d5d3d
714d794c-3e43-40a8-b752-ed04b2748016
a032dc91-28e6-49f4-bcea-1bbacf2f1a00
cfcf7c8a-5cfa-4362-ae7f-bad6bb1f4b66
e9704b2c-dbdd-4654-89a0-defe38bcadb7
6a4290f2-6397-4d58-9d20-e0b46a7d5d3d
cfcf7c8a-5cfa-4362-ae7f-bad6bb1f4b66
e9704b2c-dbdd-4654-89a0-defe38bcadb7
714d794c-3e43-40a8-b752-ed04b2748016
30792149-52da-44ec-9709-1911244bb183
15fb3a43-d8a1-4348-8e31-8f5814a38a69
1f08022d-85ba-40ba-9b79-c201c8f477b0
0eaba174-fab6-4cda-bf09-92391d586d48
a032dc91-28e6-49f4-bcea-1bbacf2f1a00

3

Answers


  1. To fetch the IDs of the users by passing the text file, modify the script like below:

    $DnPath = "C:dirnamefilename.txt"
    $DnArr = Get-Content -Path $DnPath
    $UniqueUserIds = @()
    
    foreach ($displayName in $DnArr) {
        # Get user ID using the display name
        $user = Get-MgUser -Filter "displayName eq '$displayName'" -Property id
    
        if ($user) {
            $UniqueUserIds += $user.Id
        }
    }
    $UniqueUserIds = $UniqueUserIds | Select-Object -Unique
    $UniqueUserIds
    

    enter image description here

    My text files looks like below:

    enter image description here

    Login or Signup to reply.
  2. Instead of:

    $DnArr = get-content -path $DnPath
    

    Try:

    $DnArr = get-content -path $DnPath | Select-Object -Unique
    

    Example

    Consider a file named c:tempfilename.txt with the following content:

    John
    Mary
    Peter
    Alice
    Alice
    Peter
    John
    Mary
    

    Running the following Powershell script:

    $DnPath = "c:tempfilename.txt"
    
    $DnArr = get-content -path $DnPath | Select-Object -Unique
    
    $DnArr
    

    Output is:

    John
    Mary
    Peter
    Alice
    
    Login or Signup to reply.
  3. It would be better to use -Filter instead of -Search. When using -Search, the input string you provide after displayName is split up into parts by spaces, different casing, or character types (numbers and special characters) and it will return all users where displayName contains the current string.

    $DnArr | Foreach-Object { 
      Get-Mguser -Filter "displayname eq '$_'" -ConsistencyLevel eventual -Property id | Select-Object -Property id
    }
    

    With -Filter only user(s) with exact displayName will be returned.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search