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