Skip to content

Instantly share code, notes, and snippets.

@bill-long
Last active August 4, 2021 04:40
Show Gist options
  • Save bill-long/75b0048e5cc97eb71158e7c870008cd7 to your computer and use it in GitHub Desktop.
Save bill-long/75b0048e5cc97eb71158e7c870008cd7 to your computer and use it in GitHub Desktop.
Exports all Public Folders and System Folders. If the script is interrupted, just run it again and it will resume.
[CmdletBinding()]
param (
[Parameter()]
[string[]]
$Properties = @("Identity", "EntryId", "DumpsterEntryId", "FolderSize", "FolderClass"),
[Parameter()]
[string]
$OutputFile = "$PSScriptRoot\PFHierarchy.csv"
)
$ErrorActionPreference = "Stop"
$script:progressCount = 0
$sw = New-Object System.Diagnostics.Stopwatch
$sw.Start()
$progressParams = @{
Activity = "Exporting public folder hierarchy"
}
$entryIdsAlreadyExported = New-Object 'System.Collections.Generic.HashSet[string]'
if (Test-Path $OutputFile) {
$OutputFile | Import-Csv | ForEach-Object {
[void]$entryIdsAlreadyExported.Add($_.EntryId)
$script:progressCount++
}
Write-Host "Loaded $script:progressCount folders from $OutputFile. Resuming export."
}
function GetSubfoldersRecursive {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[object]
$Folder
)
if ($sw.ElapsedMilliseconds -gt 1000) {
$sw.Restart()
Write-Progress @progressParams -CurrentOperation $Folder.Identity -Status $script:progressCount
}
if (-not $entryIdsAlreadyExported.Contains($Folder.EntryId)) {
$children = Get-PublicFolder $Folder.EntryId -GetChildren
foreach ($child in $children) {
GetSubfoldersRecursive $child
$child | Select-Object $Properties | Export-Csv $OutputFile -Append -Force
$script:progressCount++
}
}
}
$ipmSubtree = Get-PublicFolder
GetSubfoldersRecursive $ipmSubtree
$nonIpmSubtree = Get-PublicFolder \non_ipm_subtree
GetSubfoldersRecursive $nonIpmSubtree
Write-Host "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment