Created
May 4, 2021 09:33
-
-
Save BurstX/54dc38101153d2f707f0706fde3edc4f to your computer and use it in GitHub Desktop.
PowerShell (posh) script to get all the offline (taking hard-drive space) files in OneDrive local folder
This file contains 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
<# | |
.SYNOPSIS | |
Borrowed code: https://github.com/PowerShellJax/Get-FileMetaData/blob/master/Get-FileMetaDataFunction.ps1 | |
#> | |
param( | |
[Parameter(Mandatory=$true)] | |
[string] $OneDriveRootDiskPath | |
) | |
function Get-FileMetaData{ | |
[cmdletbinding()] | |
param( | |
[Parameter( | |
Mandatory = $true, | |
ValueFromPipeline = $true, | |
ValueFromPipelineByPropertyName = $true | |
)] | |
[Alias('Fullname')] | |
[string[]]$path | |
) | |
Begin{ | |
$oshell = New-Object -ComObject shell.Application | |
#$objArray = @{} | |
} | |
Process{ | |
$path | ForEach-Object{ | |
# "Processing <$_>" | |
if (Test-Path -Path $_ -PathType Leaf){ | |
$FileItem = Get-Item -Path $_ | |
$ofolder = $oshell.namespace($FileItem.DirectoryName) | |
$ofile = $ofolder.parsename($FileItem.Name) | |
$props = @{} | |
0..287 | ForEach-Object{ | |
$ExtPropName = $ofolder.GetDetailsof($ofolder.items, $_) | |
$ExtValName = $ofolder.GetDetailsof($ofile, $_) | |
if(-not $props.ContainsKey($ExtPropName) -and ($ExtValName -ne '')){ | |
$props.Add($ExtPropName,$ExtValName) | |
} | |
} | |
New-Object psobject -Property $props | Tee-Object -Variable obj | |
} | |
} | |
} | |
End{ | |
$oshell = $null | |
} | |
} | |
function FileSizeHumanReadable($sizeInBytes) | |
{ | |
$k = 1024 | |
if([int]($sizeInBytes / $k) -eq 0) { | |
return $sizeInBytes.ToString() + ' bytes'; | |
} | |
$k *= 1024 | |
if([int]($sizeInBytes / $k) -eq 0) { | |
return $($sizeInBytes / ($k / 1024)).ToString() + ' KiB' | |
} | |
$k *= 1024 | |
if([int]($sizeInBytes / $k) -eq 0) { | |
return $($sizeInBytes / ($k / 1024)).ToString() + ' MiB' | |
} | |
$k *= 1024 | |
if([int]($sizeInBytes / $k) -eq 0) { | |
return $($sizeInBytes / ($k / 1024)).ToString() + ' GiB' | |
} | |
$k *= 1024 | |
if([int]($sizeInBytes / $k) -eq 0) { | |
return $($sizeInBytes / ($k / 1024)).ToString() + ' TiB' | |
} | |
} | |
$OneDriveRootDiskPath = $OneDriveRootDiskPath.TrimEnd('\') | |
$items = Get-ChildItem "$OneDriveRootDiskPath\*" -Recurse -File | |
write-host | |
write-host '====================================' | |
write-host 'The script count disk space taken by files stored in OneDrive and downloaded locally' | |
write-host '====================================' | |
Write-Host | |
write-host 'Folder to scan: ' -NoNewline | |
write-host $([System.Uri]::new($OneDriveRootDiskPath)) -ForegroundColor Magenta | |
write-host | |
$timestampStr = Get-date -Format 'yyyyMMdd_HHmmss' | |
$scanLogFile = "$OneDriveRootDiskPath\OneDriveLocalFilesScan_$timestampStr.log" | |
write-host 'Scan log: ' -NoNewline | |
write-host $([System.Uri]::new($scanLogFile)) -ForegroundColor Cyan | |
Write-Host | |
$sumSpace = 0; | |
if($items.Length -gt 0) | |
{ | |
write-host 'Files taking space on disk:' | |
write-host '============================' | |
write-host | |
} | |
else { | |
write-host 'No files found in specified OneDrive folder' -ForegroundColor Yellow | |
exit 1; | |
} | |
$i = 1; | |
Start-Transcript -Path $scanLogFile | Out-Null | |
try { | |
foreach($item in $items) { | |
$prop = Get-FileMetaData -path $item.Fullname | |
if($prop -ne $null -and $prop.Attributes -ne $null -and ` | |
(!$prop.Attributes.Contains('O') -and !$prop.Attributes.Contains('P'))) | |
{ | |
write-host 'File path: ' -NoNewline | |
Write-Host $($item.Fullname) -ForegroundColor Cyan | |
write-host 'File URI: ' -NoNewline | |
Write-Host $([System.Uri]::new($item.Fullname)) -ForegroundColor Cyan | |
write-host 'Containing folder path: ' -NoNewline | |
Write-Host $([System.IO.Directory]::GetParent($item.Fullname)) -ForegroundColor Magenta | |
write-host 'Containing folder URI: ' -NoNewline | |
Write-Host $([System.Uri]::new([System.IO.Directory]::GetParent($item.Fullname))) -ForegroundColor Magenta | |
$sumSpace += (Get-Item $item.Fullname).Length | |
write-host "Total taken space: $(FileSizeHumanReadable -sizeInBytes $sumSpace)" | |
Write-Verbose "Attributes: $($prop.Attributes)" | |
write-host | |
Write-Progress -PercentComplete $($i++ * 100.0 / $items.Length) -Activity 'Counting disk files from OneDrive' | |
} | |
} | |
} | |
finally { | |
} | |
Stop-Transcript |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment