Streamlining OneDrive File Permissions Management with PowerShell
Efficiently managing file permissions in OneDrive is essential for ensuring that sensitive information is protected while still maintaining collaboration among team members. This PowerShell script allows administrators to automate the process of checking and updating file permissions for a specified folder in OneDrive, enabling you to control access easily and effectively. This script will: 1. Connect to the OneDrive service using the Microsoft Graph API. 2. Retrieve current permissions for a specified folder. 3. Update permissions to grant or revoke access for designated users. Utilizing this script can save time and reduce the risk of errors in permission management.
# Install the required module if not already installed
if (-Not (Get-Module -ListAvailable -Name Microsoft.Graph)) {
Install-Module Microsoft.Graph -Scope CurrentUser -AllowClobber
}
# Connect to Microsoft Graph
Connect-MgGraph -Scopes 'Files.ReadWrite.All', 'Sites.ReadWrite.All'
# Specify the folder and user details
$folderId = "your-folder-id"
$userEmail = "[email protected]"
$permissionType = "write" # Options: write, read
# Get the current permissions for the folder
$permissions = Get-MgDriveItemPermission -DriveId "your-drive-id" -ItemId $folderId
# Output current permissions
Write-Host "Current permissions for folder ID: $folderId"
$permissions | ForEach-Object { Write-Host "$($_.grantedToUser.displayName): $($_.role)" }
# Update permissions for the specified user
if ($permissionType -eq "write") {
New-MgDriveItemPermission -DriveId "your-drive-id" -ItemId $folderId -Body @{
roles = @("write")
grantedTo = @{
user@{ email = $userEmail }
}
}
Write-Host "Granted write access to $userEmail."
} elseif ($permissionType -eq "read") {
New-MgDriveItemPermission -DriveId "your-drive-id" -ItemId $folderId -Body @{
roles = @("read")
grantedTo = @{
user@{ email = $userEmail }
}
}
Write-Host "Granted read access to $userEmail."
} else {
Write-Host "Invalid permission type specified."
}
Disconnect-MgGraphRun it across your fleet
This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.
More in SharePoint & OneDrive
Monitor SharePoint Site Usage with PowerShell
2025-01-04Streamline SharePoint File Permissions Management with PowerShell
2025-01-04Managing File Permissions in SharePoint with PowerShell
2025-01-04Automate User Permissions Management in SharePoint with PowerShell
2024-12-24Efficiently List User Permissions in SharePoint with PowerShell
2024-12-24Efficiently Manage OneDrive File Permissions with PowerShell
2024-12-24Ready when you are.
Try ServerEngine free for 7 days.