Back to Browse

Create, update, license, and delete user with Microsoft Graph PowerShell SDK

1.5K views
Jun 18, 2023
8:49

This is a how-to video on how to create a new user, assign the new user a license, and then delete the new user. For connections commands please see video on 'How to connect to Microsoft Graph PowerShell SDK' Admin URLS Office 365 Admin Center: https://admin.microsoft.com/ Entra Admin Center: https://entra.microsoft.com/ #Set scope permissions before connecting #NOTE: for this video I will just be setting my scope for modifying user #Kb for permissions - https://learn.microsoft.com/en-us/graph/permissions-reference $Scopes = @( "User.ReadWrite.All" ) #Connect to Microsoft Graph PowerShell SDK Connect-MgGraph –Scope $Scopes -ForceRefresh #Check if your scopes are correct Get-MgContext | Select-Object –ExpandProperty Scopes #OR #(Get-MgContext).Scopes #view the current API endpoint version Get-MgProfile #Test to see if you are now connected properly to the tenant (Get-MgUser).DisplayName | Sort-Object | Select-Object -First 5 -Skip 1 #Create a new user account #Set the new users Password $Password = @{ Password = 'Password1' } #Create the new user New-MgUser ` -DisplayName 'New Graph User' ` -PasswordProfile $Password ` -AccountEnabled ` -MailNickName 'NewGraphUser' ` -UserPrincipalName '[email protected]' #Check if the new user was created Get-MgUser -UserId "[email protected]" | Select-Object -Property DisplayName, UserPrincipalName, Id | Format-List #Update new users display name and user principal name Update-MgUser ` -UserId '[email protected]' ` -DisplayName 'Graph User' ` -UserPrincipalName '[email protected]' #Check if the new user was update Get-MgUser -UserId "[email protected]" | Select-Object -Property DisplayName, UserPrincipalName, Id| Format-List #Assign the new user a license #KB for more information - https://learn.microsoft.com/en-us/microsoft-365/enterprise/assign-licenses-to-user-accounts-with-microsoft-365-powershell?view=o365-worldwide #Set new users usage location #ISO 3166-1 alpha-2 country code - https://support.microsoft.com/en-gb/topic/country-region-and-language-codes-add36afe-804a-44f1-ae68-cfb9c9b72f8b Update-MgUser -UserId "[email protected]" -UsageLocation US #Check if usage location was update for the new user Get-MgUser -UserId "[email protected]" | Select-Object -Property DisplayName, AssignedLicenses, UsageLocation | Format-List #Get a list of licenses currently in the tenant Get-MgSubscribedSku -All | Select-Object -Property SkuPartNumber, SkuId | Format-List #Assing the new user a license Set-MgUserLicense -UserId "[email protected]" -AddLicenses @{SkuId ="f30db892-07e9-47e9-837c-80727f46fd3d"} -RemoveLicenses @() #Check if the license was applied to the new user Get-MgUserLicenseDetail -UserId "[email protected]" | Select-Object -Property SkuPartNumber, SkuId #Remove new user license Set-MgUserLicense -UserId "[email protected]" -AddLicenses @{} -RemoveLicenses @("f30db892-07e9-47e9-837c-80727f46fd3d") #Check if license was removed from the new user Get-MgUserLicenseDetail -UserId "[email protected]" | Select-Object -Property SkuPartNumber, SkuId #Delete the new user; this does not hard delete the user it just puts them in the recycling bin Remove-MgUser -UserId "[email protected]" #Check if the new user was deleted Get-MgUser -UserId "[email protected]" #At the end disconnect from Microsoft Graph PowerShell SDK Disconnect-MgGraph | Out-Null #Test if you are now disconnected (Get-MgUser).DisplayName | Sort-Object | Select-Object -First 5 -Skip 1

Download

0 formats

No download links available.

Create, update, license, and delete user with Microsoft Graph PowerShell SDK | NatokHD