-
Notifications
You must be signed in to change notification settings - Fork 0
/
Where UserPrincipalName Starts With.ps1
46 lines (35 loc) · 1.24 KB
/
Where UserPrincipalName Starts With.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#Requires -Modules @{ ModuleName="Microsoft.Graph.Authentication"; ModuleVersion="2.3.0" }
#Requires -Modules @{ ModuleName="Microsoft.Graph.Users"; ModuleVersion="2.3.0" }
$ErrorActionPreference = 'stop'
<#
.SYNOPSIS
Get-MgUser where userPrincipalName starts with 'x'.
.DESCRIPTION
An example of how to use Get-MgUser to find all accounts with a
userPrincipalName starting with one or more prefixes.
.NOTES
AUTHOR: https://github.com/dwarfered/msgraph-sdk-powershell-examples
UPDATED: 16-09-2023
#>
$requiredScopes = @('User.Read.All')
$currentScopes = (Get-MgContext).Scopes
if ($null -eq $currentScopes) {
Connect-MgGraph -Scopes $requiredScopes | Out-Null
} elseif (($currentScopes -match ([string]::Join('|', $requiredScopes))).Count -ne $requiredScopes.Count) {
Connect-MgGraph -Scopes $requiredScopes | Out-Null
}
$upnPrefixes = @(
'svc',
'service'
)
$filter = ''
for ($i = 0; $i -lt $upnPrefixes.Count; $i++) {
if ($i -eq 0) {
$filter = "startsWith(userPrincipalName,'$($upnPrefixes[$i])')"
}
else {
$filter += " OR startsWith(userPrincipalName,'$($upnPrefixes[$i])')"
}
}
$users = Get-MgUser -All -PageSize 999 -Filter $filter
$users.UserPrincipalName