← script library

Efficiently Checking File and Folder Permissions in OneDrive with PowerShell

SharePoint & OneDriveDecember 23, 2024

Managing file and folder permissions in OneDrive is crucial for maintaining data security and ensuring that users have appropriate access rights. This PowerShell script automates the process of checking permissions for files and folders within a specific OneDrive account. It enables administrators to quickly assess access rights without the need for manual inspection. This script will: 1. Connect to the OneDrive service via Microsoft Graph. 2. Retrieve the current permissions for specified files and folders. 3. Output the permissions for easy auditing. By implementing this script, IT administrators can efficiently oversee and manage permissions in OneDrive, ensuring compliance and security within their organization.

powershell
# Install the Microsoft Graph PowerShell module if it is not already installed
if (-Not (Get-Module -ListAvailable -Name Microsoft.Graph)) {
    Install-Module -Name Microsoft.Graph -Scope CurrentUser -AllowClobber
}
# Connect to Microsoft Graph
Connect-MgGraph -Scopes 'Files.Read.All', 'Sites.Read.All'
# Define the OneDrive site and folder path
$siteId = "your-tenant-id" # Replace with your tenant id
$driveId = "your-drive-id"  # Replace with the specific drive id
$filePath = "path/to/your/file/or/folder" # Specify the file or folder path
# Retrieve item permissions
$item = Get-MgDriveItem -DriveId $driveId -ItemId $filePath -Select "id,name,permissions"
# Output the permissions
Write-Host "=== Permissions for $($item.Name) ==="
foreach ($permission in $item.Permissions) {
    Write-Host "Role: $($permission.Roles) - Granted To: $($permission.GrantedTo.SendAs)"
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Write-Host "Permissions check completed."

Run 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.

Ready when you are.

Try ServerEngine free for 7 days.