Help with Creating 365 Users and Altering UPN
I have been working on the below script for a little while and I am still having some trouble getting it to fully function. Please be kind, as this is the first script I’ve ever written.
The goal of the code is to create 365 user accounts using a .csv file. The loop should check if the upn exists, if it does not it will create the user using email address removed for privacy reasons and if it does then it will change the upn to firstname.lastname[int]@domain.com and create the user. The integer should increment up depending on how many users have a similar upn.
Right now, I can get it to create a user but it will not increment automatically. Any help would be greatly appreciated. See code below. I have switched out sensitive information that was included in the code with confidential. I set all the variables to null at the bottom in an effort to troubleshoot where the problem may be, so it is not intended to be part of the code.
Also, I would like to generate a random password. I read that I should just leave the password profile blank to complete that but it didn’t seem to work. Any suggestions with that are also welcomed.
#Connect to Microsoft Graph Powershell Module
Connect-MgGraph -Scopes “User.ReadWrite.All”,”Group.ReadWrite.All”,”Directory.ReadWrite.All”
#Connect to Exchange Online Powershell Module
Connect-ExchangeOnline
# Import the CSV file
$csvrecords = Import-Csv -Path “C:UsersConfidentialOneDrive – ConfidentialDocumentsPowerShellScriptsO365PowershellTest1.csv” #Add our file here
# Create a password profile
$PasswordProfile = @{
Password = “Password1234!”
}
# Loop through each user in the CSV file
foreach ($csvrecord in $csvrecords)
{
#Variable to increment UserPrincipalName
$i = 1
$created = $false #set the variable to end the loop
$user = Get-MgUser -Filter “UserPrincipalName eq ‘$upn'” #Set User variable to User’s GUID
do {
If(!$User.ID)
{
$upn = $csvrecord.UserPrincipalName #Set the UPN variable to the user’s email address
# Create a new user if the username does not exist
$newUser = New-MgUser -GivenName $csvrecord.FirstName -Surname $csvrecord.LastName -DisplayName $csvrecord.DisplayName -UserPrincipalName $upn -UsageLocation ‘US’ -JobTitle $csvrecord.JobTitle -Department $csvrecord.Department -PasswordProfile $PasswordProfile -MailNickname $csvrecord.MailNickname -AccountEnabled
$created = $true
}
else {
$i = $i + 1
#Add an interger to the end of first and last name in username
$upn = $csvrecord.FirstName + “.” + $csvrecord.LastName + $i + “@Confidential.org”
#create the user
$newUser = New-MgUser -GivenName $csvrecord.FirstName -Surname $csvrecord.LastName -DisplayName $csvrecord.DisplayName -UserPrincipalName $upn -UsageLocation ‘US’ -JobTitle $csvrecord.JobTitle -Department $csvrecord.Department -PasswordProfile $PasswordProfile -MailNickname $csvrecord.MailNickname -AccountEnabled
$created = $true
}
} until ($created = $true)
# Assign a license to the new user
$e3Sku = Get-MgSubscribedSku | Where SkuPartNumber -eq ‘SPE_E3’
Set-MgUserLicense -UserID $newUser.UserPrincipalName -AddLicenses @{SkuId = $e3Sku.SkuId} -RemoveLicenses @()
# Export the results to a CSV file
$csvrecords | Export-Csv -Path “C:UsersConfidentialOneDrive -ConfidentialDocumentsPowerShellScriptsO365NewAccountResultsTest1.csv” -NoTypeInformation #Add our file here
$i = $null
$created = $null
$upn = $null
$user = $null
}
#Disconnect from Microsoft Exchange
Disconnect-ExchangeOnline
#Disconnect from MgGraph
Disconnect-MgGraph
I have been working on the below script for a little while and I am still having some trouble getting it to fully function. Please be kind, as this is the first script I’ve ever written. The goal of the code is to create 365 user accounts using a .csv file. The loop should check if the upn exists, if it does not it will create the user using email address removed for privacy reasons and if it does then it will change the upn to firstname.lastname[int]@domain.com and create the user. The integer should increment up depending on how many users have a similar upn. Right now, I can get it to create a user but it will not increment automatically. Any help would be greatly appreciated. See code below. I have switched out sensitive information that was included in the code with confidential. I set all the variables to null at the bottom in an effort to troubleshoot where the problem may be, so it is not intended to be part of the code.Also, I would like to generate a random password. I read that I should just leave the password profile blank to complete that but it didn’t seem to work. Any suggestions with that are also welcomed. #Connect to Microsoft Graph Powershell ModuleConnect-MgGraph -Scopes “User.ReadWrite.All”,”Group.ReadWrite.All”,”Directory.ReadWrite.All” #Connect to Exchange Online Powershell ModuleConnect-ExchangeOnline # Import the CSV file$csvrecords = Import-Csv -Path “C:UsersConfidentialOneDrive – ConfidentialDocumentsPowerShellScriptsO365PowershellTest1.csv” #Add our file here # Create a password profile $PasswordProfile = @{ Password = “Password1234!” } # Loop through each user in the CSV fileforeach ($csvrecord in $csvrecords) { #Variable to increment UserPrincipalName $i = 1 $created = $false #set the variable to end the loop $user = Get-MgUser -Filter “UserPrincipalName eq ‘$upn'” #Set User variable to User’s GUID do { If(!$User.ID) { $upn = $csvrecord.UserPrincipalName #Set the UPN variable to the user’s email address # Create a new user if the username does not exist $newUser = New-MgUser -GivenName $csvrecord.FirstName -Surname $csvrecord.LastName -DisplayName $csvrecord.DisplayName -UserPrincipalName $upn -UsageLocation ‘US’ -JobTitle $csvrecord.JobTitle -Department $csvrecord.Department -PasswordProfile $PasswordProfile -MailNickname $csvrecord.MailNickname -AccountEnabled $created = $true} else { $i = $i + 1 #Add an interger to the end of first and last name in username $upn = $csvrecord.FirstName + “.” + $csvrecord.LastName + $i + “@Confidential.org” #create the user $newUser = New-MgUser -GivenName $csvrecord.FirstName -Surname $csvrecord.LastName -DisplayName $csvrecord.DisplayName -UserPrincipalName $upn -UsageLocation ‘US’ -JobTitle $csvrecord.JobTitle -Department $csvrecord.Department -PasswordProfile $PasswordProfile -MailNickname $csvrecord.MailNickname -AccountEnabled $created = $true } } until ($created = $true) # Assign a license to the new user $e3Sku = Get-MgSubscribedSku | Where SkuPartNumber -eq ‘SPE_E3’ Set-MgUserLicense -UserID $newUser.UserPrincipalName -AddLicenses @{SkuId = $e3Sku.SkuId} -RemoveLicenses @() # Export the results to a CSV file$csvrecords | Export-Csv -Path “C:UsersConfidentialOneDrive -ConfidentialDocumentsPowerShellScriptsO365NewAccountResultsTest1.csv” -NoTypeInformation #Add our file here $i = $null$created = $null$upn = $null$user = $null } #Disconnect from Microsoft ExchangeDisconnect-ExchangeOnline #Disconnect from MgGraphDisconnect-MgGraph Read More