Last active
April 12, 2025 17:56
-
-
Save joegasper/6ec54d4e90cc25b06ff71c5d3b740209 to your computer and use it in GitHub Desktop.
Get-OneDriveFileInfo.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Connect to the SharePoint site | |
Connect-PnPOnline -Url "YourSiteURL" -Credentials | |
# Specify the name of your Document Library | |
$DocLib = 'Documents' | |
# Retrieve all items from the Document Library in batches | |
$ListItems = Get-PnPListItem -List $DocLib -PageSize 2000 -ScriptBlock { | |
Param($items) | |
$items.Context.ExecuteQuery() | |
} | |
# Loop through each item and collect necessary details | |
foreach ($Item in $ListItems) { | |
if ($Item.FieldValues["Modified"] -lt (Get-Date).AddYears(-7)) { | |
New-Object PSObject -Property @{ | |
FileName = $Item.FieldValues["FileLeafRef"] | |
FileID = $Item.FieldValues["UniqueId"] | |
FileType = $Item.FieldValues["File_x0020_Type"] | |
RelativeURL = $Item.FieldValues["FileRef"] | |
CreatedByUPN = $Item.FieldValues["Author"].Email | |
CreatedTime = $Item.FieldValues["Created"] | |
LastModifiedTime = $Item.FieldValues["Modified"] | |
ModifiedByUPN = $Item.FieldValues["Editor"].Email | |
FileSize_KB = [Math]::Round(($Item.FieldValues["File_x0020_Size"] / 1024), 2) | |
} | |
} | |
} | |
Disconnect-PnPOnline | |
# Import the required module | |
Import-Module Microsoft.Graph | |
# Connect to Microsoft Graph | |
Connect-MgGraph -Scopes "Files.ReadWrite.All" | |
$UPN = '[email protected]' | |
$Ddrive = Get-MgUserDefaultDrive -UserId $UPN | |
# Get all files in the OneDrive account that are older than 5 years | |
$files = Get-MgDriveListItem -DriveId $Ddrive.Id | Where-Object { $_.ContentType.Name -ne 'Folder' -and $_.LastModifiedDateTime -lt (Get-Date).AddYears(-5) } | |
# Loop through all pages | |
do { | |
# Process each item in the current page | |
foreach ($file in $files) { | |
Write-Output ("FileName: " + $file.name) | |
Write-Output ("FileID: " + $file.id) | |
Write-Output ("FileType: " + $file.file.mimeType) | |
Write-Output ("RelativeURL: " + $file.parentReference.path) | |
Write-Output ("CreatedByEmail: " + $file.createdBy.User.AdditionalProperties.email) | |
Write-Output ("ModifiedByEmail: " + $file.LastModifiedBy.User.AdditionalProperties.email) | |
Write-Output ("CreatedTime: " + $file.createdDateTime) | |
Write-Output ("LastModifiedTime: " + $file.lastModifiedDateTime) | |
Write-Output ("FileSize_KB: " + $file.size / 1024) | |
} | |
# Get the next page | |
$files = $files | Get-MgNext | |
} while ($files) | |
# Loop through each file | |
foreach ($file in $files) { | |
# Print the file's properties | |
Write-Output ("FileName: " + $file.name) | |
Write-Output ("FileID: " + $file.id) | |
Write-Output ("FileType: " + $file.file.mimeType) | |
Write-Output ("RelativeURL: " + $file.parentReference.path) | |
Write-Output ("CreatedByEmail: " + $file.createdBy.User.AdditionalProperties.email) | |
Write-Output ("ModifiedByEmail: " + $file.LastModifiedBy.User.AdditionalProperties.email) | |
Write-Output ("CreatedTime: " + $file.createdDateTime) | |
Write-Output ("LastModifiedTime: " + $file.lastModifiedDateTime) | |
Write-Output ("FileSize_KB: " + $file.size / 1024) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment