How to disconnect PST file from Outlook using Powershell?

泪湿孤枕 提交于 2019-12-01 10:16:20

问题


I'm trying to write a script to disconnect PSTs files from Outlook.

I've been trying with something like this:

$Outlook = new-object -com outlook.application 
$Namespace = $Outlook.getNamespace("MAPI")

$PSTtoDelete = "c:\test\pst.pst"

$Namespace.RemoveStore($PSTtoDelete)

I get the following error:

"Cannot Find overload for "RemoveStore" and the argument count "1".

I also tried a different solution with this (found here http://www.mikepfeiffer.net/2013/04/how-to-test-outlook-pst-personal-folder-file-access-with-powershell/) :

$namespace.GetType().InvokeMember('RemoveStore',[System.Reflection.BindingFlags]::InvokeMethod,$null,$namespace,($PSTFolder))

I took a look to the technect documentations and if I understand properly the RemoveStore Method expects a Folder.

If anyone would be able to give me a hint on this one that would be greatly appreciated!

Thanks!


回答1:


According to your link the script expects Name of the attached PST, not the path. Try this:

$Outlook = new-object -com outlook.application 
$Namespace = $Outlook.getNamespace("MAPI")

$PSTtoDelete = "c:\test\pst.pst"
$PST = $namespace.Stores | ? {$_.FilePath -eq $PSTtoDelete}
$PSTRoot = $PST.GetRootFolder()


$PSTFolder = $namespace.Folders.Item($PSTRoot.Name)
$namespace.GetType().InvokeMember('RemoveStore',[System.Reflection.BindingFlags]::InvokeMethod,$null,$namespace,($PSTFolder))



回答2:


To remove ALL .pst files:

$Outlook = New-Object -ComObject Outlook.Application
$Namespace = $Outlook.getNamespace("MAPI")

$all_psts = $Namespace.Stores | Where-Object {($_.ExchangeStoreType -eq '3') -and ($_.FilePath -like '*.pst') -and ($_.IsDataFileStore -eq $true)}

ForEach ($pst in $all_psts){
    $Outlook.Session.RemoveStore($pst.GetRootFolder())
}


来源:https://stackoverflow.com/questions/24058514/how-to-disconnect-pst-file-from-outlook-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!