Last active
September 12, 2017 06:28
-
-
Save chrisbrownie/d94dfc8bc7d3730edd4585e8e835b776 to your computer and use it in GitHub Desktop.
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
Param( | |
[String]$Path | |
) | |
$pfShortName = ($Path -replace "^\\\\[A-Za-z0-0@\-_. ]*\\All Public Folders\\","").Replace("\","_") | |
$FilePath = Join-Path -Path (Get-Location) -ChildPath "$pfShortName.pst" | |
$outlook = New-Object -ComObject Outlook.Application | |
$namespace = $Outlook.GetNamespace("MAPI") | |
$PublicFolders = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olPublicFoldersAllPublicFolders) | |
# Work down the tree to get to the right folder (remove empty values) | |
$folderhierarchy = $Path.Split("\") | Where-Object { $_ } | |
$thisfolder = $PublicFolders | |
foreach ($folder in $folderhierarchy | Select-Object -Skip 2) { | |
$thisFolder = $thisFolder.Folders | Where-Object { $_.Name -eq $folder } | |
} | |
$Outlook.Session.AddStore($FilePath) | |
$PST = $namespace.Stores | Where-Object { $_.FilePath -eq $FilePath } | |
$thisFolder.CopyTo($PST) | Out-Null | |
$pstRootFolder = $pst.GetRootFolder() | |
$namespace.GetType().InvokeMember('RemoveStore',[System.Reflection.BindingFlags]::InvokeMethod,$null,$Namespace,$pstRootFolder) | |
# Close Outlook | |
$Outlook.Quit() | |
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($Outlook) | |
Remove-Variable Namespace | |
Remove-Variable Outlook | |
[GC]::Collect() | Out-Null | |
[GC]::WaitForPendingFinalizers() | Out-Null | |
Get-Process Outlook | Stop-Process |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment