Graph PowerShell Script for Creating Microsoft 365 User and Assigning License
Here's a Graph PowerShell script that helps Microsoft 365 administrators to create users and assign M365 license.
Preqrequisites
- Microsoft 365 Admin Access: You need administrative privileges on a Microsoft 365 account to create users and assign licenses.
- PowerShell: Ensure PowerShell is installed on your computer. This script is compatible with PowerShell 5.1 or higher.
- Microsoft Graph PowerShell Module: The Microsoft Graph PowerShell module must be installed. If it is not installed, you can install it by running Install-Module -Name Microsoft.Graph -Force -AllowClobber in PowerShell as an administrator.
-
Required Graph API Permissions: Required permissions are "User.ReadWrite.All", "Directory.ReadWrite.All"
Script for your Reference
How to Execute the Script?
Heres how you can execute the script:
- Open PowerShell: Open your PowerShell application. If you are on Windows, you can search for PowerShell in your Start menu, right-click it, and select "Run as administrator".
- Navigate to the Script Location: Use the cd command to change the directory to where your script is located. For example, if your script is in your Downloads folder, you might use: cd C:\users\yourusername\downloads
- Run the Script: Execute the script by typing ./<ScriptName.ps1> and pressing Enter. Replace "ScriptName.ps1" with the actual name of the script file.
-
Enter User Details: The script will prompt you to enter the following details:
- User Email: Enter the full email address for the new user. Example: john.doe@yourcompany.com
- Display Name: Enter the display name for the user. Example: John Doe
- Password: Enter a password for the user. Ensure it meets your organization's security requirements.
- License SKU ID: Enter the SKU ID of the license you wish to assign to the user. Example: c42b9cae-ea4f-4ab7-9717-81576235ccac
Note: Run Get-MgSubscribedSku -All command to get the license SKU IDs in your M365 tenant.
- Script Execution: After entering the details, the script will connect to Microsoft Graph, create the user, and assign the specified license. Messages will be displayed in the PowerShell window indicating the progress and the outcome of each step.
- Verify the User Creation: You can verify that the user has been created and the license assigned by logging into your Microsoft 365 admin center and checking under Users >> Active Users.
Further Enhancing the Script
Enhancing the script to create users and assign licenses in Microsoft 365 using the Graph PowerShell SDK can involve several additions and improvements to make it more robust, user-friendly, and flexible. Here are some enhancements you might consider:
- Parameter Validation: Adding checks to validate the parameters before attempting to execute actions can prevent common errors, such as invalid email formats or missing information.
- Improved Error Handling: Enhance the error handling to catch more specific errors and provide clearer messages to the user. This makes troubleshooting easier.
- Automating License SKU ID Selection: Allow users to select from a list of available licenses instead of manually entering the SKU ID. This can be achieved by fetching available licenses and letting the user choose.
- User Feedback and Confirmation: Before performing irreversible actions, ask for user confirmation. Provide summaries of actions for user review.
- Interactive Menus: For a more interactive experience, especially when running the script manually, consider adding a menu system that guides the user through different options and configurations.
Related Articles:
Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell