Remove PST from Outlook with PowerShell
-
Monday, February 27, 2012 8:21 PM
I created the following in order to remove all stores that do not include the store names in the IF statement below. It will remove the first two stores then error out stating that "array index out of bounds". Not sure what is going on since if you comment out the line that says "$outlook.session.removestore($pst)" it goes thorugh the loop fine and prints out the name of the store and the index variable $i. Any help is greatly appreciated.
$outlook = new-object -comobject Outlook.Application
$objFolders = $outlook.Session.Folders
for ($i=1;$i -le $objFolders.Count;$i++)
{
$pst = $objFolders.Item($i)
if (($pst.Name -notlike "*cisco*") -and ($pst.Name -notlike "*sharepoint*") -and ($pst.Name -notlike "*gmail*") `
-and ($pst.Name -notlike "*yahoo*") -and ($pst.Name -notlike "*public*") -and ($pst.Name -notlike "*archive*") `
-and ($pst.Name -notlike "*mailbox*"))
{
$outlook.Session.RemoveStore($pst)
}
write-host $pst.Name' $i ='$i
}
write-host "Complete!"
Corey Radford | IT Client Services Dixon Hughes Goodman LLP D 704.367.7049 T 704.367.7020 6525 Morrison Boulevard, Suite 500, Charlotte, NC 28211 Corey.Radford@dhgllp.com
- Moved by Max MengMicrosoft Contingent Staff Tuesday, February 28, 2012 2:20 AM Moving to a more appropriate forum (From:Outlook IT Pro Discussions)
All Replies
-
Tuesday, February 28, 2012 2:43 AM
You cannot remove the primary store.
¯\_(ツ)_/¯
-
Tuesday, February 28, 2012 1:43 PM
I am not trying to remove the primary store. Since we use exchange my primary store is not a PST. in the IF statement I exclude all the stores that I do not want to run the RemoveStore() method on. After attacking it another way I was able to come up with the following that works.
$outlook
= New-Object-ComObjectOutlook.Application
$namespace
= $outlook.getnamespace("MAPI")
$myArray
= @()
$outlook
.Session.Stores | `
where{ ($_.FilePath -like'*.PST') -and($_.FilePath -notlike"*cisco*") -and($_.FilePath -notlike"*gmail*") `
-and($_.FilePath -notlike"*yahoo*") -and($_.FilePath -notlike"*sharepoint*") `
-and($_.FilePath -notlike"*hotmail*") -and($_.FilePath -notlike"*carolinarr*") `
-and($_.FilePath -notlike"*.edu") } | foreach{[array]$myArray+= $_.FilePath}
for
($x=0;$x-le$myArray.length-1;$x++)
{
$PSTPath= $myArray[$x]
$PST= $namespace.Stores | ?{$_.FilePath -like$PSTPath}
$PSTRoot= $PST.GetRootFolder() #Get Root Folder name of PST
$PSTFolder= $Namespace.Folders.Item($PSTRoot.Name) #Bind to PST for disconnection
$Namespace.GetType().InvokeMember('RemoveStore',[System.Reflection.BindingFlags]::InvokeMethod,$null,$Namespace,($PSTFolder)) #Disconnect .PST
}
Corey Radford | IT Client Services Dixon Hughes Goodman LLP D 704.367.7049 T 704.367.7020 6525 Morrison Boulevard, Suite 500, Charlotte, NC 28211 Corey.Radford@dhgllp.com
-
Tuesday, February 28, 2012 1:44 PM
I am not trying to remove the primary store. Since we use exchange my primary store is not a PST. in the IF statement I exclude all the stores that I do not want to run the RemoveStore() method on. After attacking it another way I was able to come up with the following that works.
$outlook
= New-Object-ComObjectOutlook.Application
$namespace
= $outlook.getnamespace("MAPI")
$myArray
= @()
$outlook
.Session.Stores | `
where{ ($_.FilePath -like'*.PST') -and($_.FilePath -notlike"*cisco*") -and($_.FilePath -notlike"*gmail*") `
-and($_.FilePath -notlike"*yahoo*") -and($_.FilePath -notlike"*sharepoint*") `
-and($_.FilePath -notlike"*hotmail*") -and($_.FilePath -notlike"*carolinarr*") `
-and($_.FilePath -notlike"*.edu") } | foreach{[array]$myArray+= $_.FilePath}
for
($x=0;$x-le$myArray.length-1;$x++)
{
$PSTPath= $myArray[$x]
$PST= $namespace.Stores | ?{$_.FilePath -like$PSTPath}
$PSTRoot= $PST.GetRootFolder() #Get Root Folder name of PST
$PSTFolder= $Namespace.Folders.Item($PSTRoot.Name) #Bind to PST for disconnection
$Namespace.GetType().InvokeMember('RemoveStore',[System.Reflection.BindingFlags]::InvokeMethod,$null,$Namespace,($PSTFolder)) #Disconnect .PST
}
Corey Radford | IT Client Services Dixon Hughes Goodman LLP D 704.367.7049 T 704.367.7020 6525 Morrison Boulevard, Suite 500, Charlotte, NC 28211 Corey.Radford@dhgllp.com
Wow, I guess I really should hae re-written that.Corey Radford | IT Client Services Dixon Hughes Goodman LLP D 704.367.7049 T 704.367.7020 6525 Morrison Boulevard, Suite 500, Charlotte, NC 28211 Corey.Radford@dhgllp.com
- Marked As Answer by IamMredMicrosoft Employee, Owner Thursday, March 01, 2012 3:51 AM

