如何使用Powershell从Outlook断开PST文件?

时间:2014-06-05 11:03:31

标签: powershell outlook pst

我正在尝试编写脚本以断开Outlook中的PST文件。

我一直在尝试这样的事情:

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

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

$Namespace.RemoveStore($PSTtoDelete)

我收到以下错误:

  

"无法找到" RemoveStore"和参数计数" 1"。

我也尝试了另一种解决方案(在这里找到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))

我查看了technect documentations,如果我理解正确,则RemoveStore方法需要一个文件夹。

如果有人能够给我一个暗示,我会非常感激!

谢谢!

2 个答案:

答案 0 :(得分:2)

根据您的链接,脚本需要连接的PST的名称,而不是路径。试试这个:

$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))

答案 1 :(得分:1)

删除所有.pst文件:

$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())
}