-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateUserMultipleUser(Prompt).ps1
More file actions
50 lines (40 loc) · 1.74 KB
/
CreateUserMultipleUser(Prompt).ps1
File metadata and controls
50 lines (40 loc) · 1.74 KB
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
47
48
49
50
# Connect to services
Connect-MgGraph
Connect-MsolService
# Function to create a new user
function New-User {
param (
[string]$fname,
[string]$lname,
[string]$username,
[string]$displayName,
[SecureString]$password
)
$email = "$username@sociabble.com"
# Check if the user already exists
$existingUser = Get-MsolUser -UserPrincipalName $email -ErrorAction SilentlyContinue
if ($existingUser) {
Write-Host "User $email already exists. Skipping creation."
return
}
# Create new user
New-MsolUser -DisplayName "$displayName" -FirstName "$fname" -LastName "$lname" -UserPrincipalName $email -UsageLocation FR -Password $password -BlockCredential $true
if ($?) {
Write-Host "User $email created successfully" -ForegroundColor Green
}
$UserCreated = Get-MsolUser -UserPrincipalName $email
update-mguser -UserId $UserCreated.ObjectId -OtherMails $email
# Additional operations like assigning license and adding to group can be included here
}
# Loop to create multiple users
do {
$fname = Read-Host -Prompt 'Write the First Name'
$lname = Read-Host -Prompt 'Write the Last Name'
$username = Read-Host -Prompt 'Write the Username (First part of email address - ie: Jane.Doe)'
$displayName = Read-Host -Prompt 'Write the Display Name'
# Convert the password to SecureString
$securePassword = Read-Host -Prompt "Enter the password of the user to add" -AsSecureString
New-User -fname $fname -lname $lname -username $username -displayName $displayName -password $securePassword
$continue = Read-Host -Prompt 'Do you want to add another user? (yes/no)'
} while ($continue -eq 'yes')
Disconnect-MgGraph