Efficiently Manage OneDrive File Permissions with PowerShell

In this post, we will present a PowerShell script that simplifies the management of file permissions within OneDrive for Business. Properly managing file permissions is crucial for collaboration and data security in any organization. This script allows administrators to quickly assign or remove access for users to specific files in OneDrive, ensuring that sensitive information is shared appropriately.
Here is the PowerShell script for managing OneDrive file permissions:

# Connect to SharePoint Online
$tenantAdminUrl = "https://yourtenant-admin.sharepoint.com"
$credential = Get-Credential
Connect-SPOService -Url $tenantAdminUrl -Credential $credential
# Define the OneDrive file and user details
$fileUrl = "https://yourtenant-my.sharepoint.com/personal/username_example_com/Documents/YourFile.docx"
$userEmail = "[email protected]"
$permissionLevel = "Edit"  # Options: "View", "Edit"
# Set permissions on the OneDrive file
Set-SPOUser -Site $fileUrl -LoginName $userEmail -IsSiteCollectionAdmin $true
Set-SPOSharingLink -Path $fileUrl -LinkType $permissionLevel -User $userEmail
Write-Host "Permissions have been updated for $userEmail on the file $fileUrl with $permissionLevel access."