Disconnect all PSTs from the Default Outlook Profile

Today, one of my consultant peers posed a problem to me: a customer wanted to import all PSTs into Office 365 archives, but in order to do that, had to disconnect them from the user's default Outlook profile.

Fortunately, you can expose a number of methods and properties from the Outlook ComObject inside PowerShell, so this wasn't terribly difficult (although, I'm still trying to figure out how to do this against all Outlook profiles configured).

First, we can instantiate the Outlook object using this simple process:

$Outlook = New-Object -ComObject Outlook.Application

The data we need to access is stored inside the Outlook namespace object. According to https://msdn.microsoft.com/en-us/library/office/ff869848.aspx, we do that this way:

$NameSpace = $Outlook.GetNameSpace("MAPI")
If we examine the available methods and properties, we can see that there is a property called "Stores."  Stores refers to the locations or services that Outlook uses to (wait for it) store data.   Seems like this might be the right place.

Digging into Stores shows us what we're really after.  To examine the data services connected to the default Outlook profile, we can run $NameSpace.Stores:

Here, we have two ways to identify the objects that we're looking for.  The two important properties that we can look for are the ExchangeStoreType and the FilePath.  For PSTs, the ExchangeStoreType is 3 (in case someone has decided to rename the PST suffix).  From here, it's just a matter of connecting to the profile and calling the "RemoveStore" method.

So, put all those things together:

Head on over to the TechNet Gallery to download this script.