-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateAzureADUser.ps1
29 lines (25 loc) · 1.33 KB
/
CreateAzureADUser.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
if (-not (Get-Module -ListAvailable -Name AzureAD)) {
Install-Module -Name AzureAD -Force -Scope CurrentUser
}
Connect-AzureAD
$UserPrincipalName = Read-Host "Enter User Principal Name (e.g., user@domain.com)"
$DisplayName = Read-Host "Enter Display Name (e.g., John Doe)"
$MailNickname = Read-Host "Enter Mail Nickname (e.g., johndoe)"
$Password = Read-Host "Enter Password (must meet Azure AD complexity requirements)"
$ForcePasswordChange = Read-Host "Force password change on first login? (yes/no)"
$ForcePasswordChangeBool = if ($ForcePasswordChange -eq "yes") { $true } else { $false }
$AccountEnabled = Read-Host "Enable account immediately? (yes/no)"
$AccountEnabledBool = if ($AccountEnabled -eq "yes") { $true } else { $false }
$PasswordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
$PasswordProfile.Password = $Password
$PasswordProfile.ForceChangePasswordNextLogin = $ForcePasswordChangeBool
try {
New-AzureADUser -UserPrincipalName $UserPrincipalName `
-DisplayName $DisplayName `
-MailNickname $MailNickname `
-PasswordProfile $PasswordProfile `
-AccountEnabled $AccountEnabledBool
Write-Host "User created successfully!" -ForegroundColor Green
} catch {
Write-Host "Error creating user: $_" -ForegroundColor Red
}