Automating User Onboarding with PnP PowerShell

Loading

User onboarding in Microsoft 365 and SharePoint Online involves multiple steps, such as:

  • Creating user accounts
  • Assigning licenses and permissions
  • Adding users to SharePoint groups
  • Creating OneDrive storage
  • Setting up default files and folders
  • Granting access to SharePoint sites
  • Sending welcome emails

With PnP PowerShell, you can automate these tasks, reducing manual effort and ensuring a consistent onboarding process.


Step 1: Install and Connect PnP PowerShell

Before running any commands, install PnP PowerShell if you haven’t already:

Install-Module -Name PnP.PowerShell -Scope CurrentUser -Force

Then, connect to Microsoft 365:

$AdminUrl = "https://yourtenant-admin.sharepoint.com"
Connect-PnPOnline -Url $AdminUrl -Interactive

Connected to SharePoint Online!


Step 2: Create a New Microsoft 365 User

To create a new user, use Microsoft Graph PowerShell (PnP PowerShell does not handle user creation directly).

$UserPrincipalName = "newuser@yourtenant.onmicrosoft.com"
$DisplayName = "New Employee"
$PasswordProfile = @{
Password = "P@ssword1234"
ForceChangePasswordNextSignIn = $true
}

New-MgUser -UserPrincipalName $UserPrincipalName `
-DisplayName $DisplayName `
-MailNickname "newuser" `
-AccountEnabled $true `
-PasswordProfile $PasswordProfile

User account created!


Step 3: Assign a Microsoft 365 License

To assign a license, first get the available SKU IDs:

Get-MgSubscribedSku | Select-Object SkuId, SkuPartNumber

Example output:

SkuId                                SkuPartNumber
----- --------------
6fd2c87f-b296-42f0-b197-1e91e994b900 ENTERPRISEPACK

Now, assign the license:

$License = @{
"SkuId" = "6fd2c87f-b296-42f0-b197-1e91e994b900"
}

Set-MgUserLicense -UserId $UserPrincipalName -AddLicenses $License -RemoveLicenses @()

Microsoft 365 license assigned!


Step 4: Add User to SharePoint Groups

To add the user to a SharePoint site group, first get the Group ID:

$SiteUrl = "https://yourtenant.sharepoint.com/sites/YourSite"
Connect-PnPOnline -Url $SiteUrl -Interactive

Get-PnPGroup | Select Title, Id

Example output:

Title                  Id
-------------------- ----
Members 5
Owners 3
Visitors 7

Now, add the user to the Members group:

$GroupId = 5  # Replace with actual group ID
Add-PnPGroupMember -LoginName $UserPrincipalName -Group $GroupId

User added to SharePoint group!


Step 5: Create a OneDrive Storage for the User

Run the following command to provision OneDrive storage:

Request-SPOPersonalSite -UserEmails $UserPrincipalName

OneDrive created for the user!


Step 6: Create Default Folders and Files in OneDrive

After provisioning OneDrive, upload default files:

$OneDriveUrl = "https://yourtenant-my.sharepoint.com/personal/newuser_yourtenant_onmicrosoft_com/Documents"
Connect-PnPOnline -Url $OneDriveUrl -Interactive

# Create a default folder
New-PnPListItem -List "Documents" -Values @{"FileLeafRef"="Welcome Documents"}

# Upload a welcome PDF
Add-PnPFile -Path "C:\Templates\Welcome.pdf" -Folder "Documents/Welcome Documents"

Default files uploaded to OneDrive!


Step 7: Assign SharePoint Site Permissions

To grant direct site access, run:

Grant-PnPAzureADUserSitePermission -LoginName $UserPrincipalName -Site $SiteUrl -Roles "Read"

User granted read access to SharePoint!


Step 8: Send a Welcome Email

Now, send a welcome email using Power Automate or SMTP:

$SMTPServer = "smtp.office365.com"
$SMTPPort = "587"
$From = "admin@yourtenant.com"
$To = $UserPrincipalName
$Subject = "Welcome to the Company!"
$Body = @"
Hello $DisplayName,

Welcome to the company! Your account is set up.

Here are your details:
- Email: $UserPrincipalName
- SharePoint Site: $SiteUrl
- OneDrive: $OneDriveUrl

Let us know if you need help.

Best,
IT Team
"@

Send-MailMessage -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl `
-Credential (Get-Credential) -From $From -To $To -Subject $Subject -Body $Body

Welcome email sent!


Step 9: Automate the Process Using a Script

To onboard multiple users at once, save the script as OnboardUsers.ps1 and run it in bulk:

$Users = Import-Csv "C:\UsersList.csv"  # CSV file containing UserPrincipalName and DisplayName

foreach ($User in $Users) {
$UserPrincipalName = $User.UserPrincipalName
$DisplayName = $User.DisplayName

# Create User, Assign License, Add to SharePoint Groups, etc.
Write-Host "Onboarding $DisplayName..."
}

Bulk onboarding automated!

Leave a Reply

Your email address will not be published. Required fields are marked *