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
To fetch the IDs of the users by passing the text file, modify the script like below:
My text files looks like below:
Instead of:
Try:
Example
Consider a file named
c:tempfilename.txt
with the following content:Running the following Powershell script:
Output is:
It would be better to use
-Filter
instead of-Search
. When using-Search
, the input string you provide afterdisplayName
is split up into parts by spaces, different casing, or character types (numbers and special characters) and it will return all users wheredisplayName
contains the current string.With
-Filter
only user(s) with exactdisplayName
will be returned.