Streamlining OneDrive File Management with PowerShell
Effectively managing files in OneDrive is essential for ensuring collaboration and security within an organization. This PowerShell script automates the process of retrieving and managing files in a OneDrive account, making it easier for administrators to assess file status and permissions. This script will: 1. Connect to OneDrive using Microsoft Graph. 2. Retrieve a list of files and their current permissions in a specified OneDrive directory. 3. Provide options for displaying or updating file permissions. By using this script, IT administrators can enhance their ability to monitor and manage files in OneDrive efficiently.
# Install Microsoft Graph PowerShell module if 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-site-id" # Replace with your SharePoint site ID
$driveId = "your-drive-id" # Replace with your OneDrive drive ID
# Retrieve all files in the specified OneDrive directory
$files = Get-MgDriveItem -DriveId $driveId -ItemId "root/children"
Write-Host "=== Files in OneDrive ==="
foreach ($file in $files) {
Write-Host "File Name: $($file.Name) - ID: $($file.Id) - Size: $($file.Size) bytes"
$permissions = Get-MgDriveItemPermission -DriveId $driveId -ItemId $file.Id
Write-Host "Permissions: "
foreach ($perm in $permissions) {
Write-Host " Role: $($perm.Roles) - Granted To: $($perm.GrantedTo.User.DisplayName)"
}
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Write-Host "File management process 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.
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.