How to disconnect PST file from Outlook using Powershell?

后端 未结 2 1554
情话喂你
情话喂你 2021-01-16 12:47

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

I\'ve been trying with something like this:



        
相关标签:
2条回答
  • 2021-01-16 13:33

    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())
    }
    
    0 讨论(0)
  • 2021-01-16 13:36

    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))
    
    0 讨论(0)
提交回复
热议问题