Reputation: 1
I am using the below script to remove all PSTs from an Outlook profile, then delete the PST files. However, it seems that this can only grab the default mail profile.
$Outlook = New-Object -ComObject Outlook.Application
$Namespace = $Outlook.getNamespace("MAPI")
$folderCheckVAR = Get-Item -Path "C:\temp" -ea Ignore
if ($folderCheckVAR.Exists)
{
Remove-Item "C:\temp\PST_Paths.csv" -ea Ignore
}
else
{
New-Item -Path "C:\" -Name "temp" -ItemType "directory" -ea Ignore
}
$all_psts = $Namespace.Stores | Where-Object {($_.ExchangeStoreType -eq '3') -and
($_.FilePath -like '*.pst') -and ($_.IsDataFileStore -eq $true)}
ForEach ($pst in $all_psts){
$outputVAR = [pscustomobject]@{
"FilePath" = $pst.FilePath
}
$outputVAR | export-csv -NoTypeInformation -Append -Path "c:\temp\Pst_Paths.csv"
$Outlook.Session.RemoveStore($pst.GetRootFolder())
}
if (Get-Process Outlook -ea SilentlyContinue) {
Get-Process Outlook | Foreach-Object { $_.CloseMainWindow() | Out-Null } | Stop-
Process -Force
}
#Delay between Outlook close and file deletion to ensure Outlook closed completely
Start-Sleep -Seconds 5
$csv = "C:\temp\PST_Paths.csv"
$pstPaths = Import-CSV $csv
foreach ($path in $pstPaths) {
Remove-Item $path.FilePath
}
These three lines are what grab the mail profile and grab the PSTs:
$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)}
Is there a way that I can adjust this be able to get all mail profiles on the user account, not just the default?
Upvotes: 0
Views: 697
Reputation: 66215
Keep in mind that Outlook is a singleton - only one instance of outlook.exe can run at any given time. You can loop through all profiles, start Outlook (by creating an instance of the Outlook.Application
object), log to the given profile by calling Application.GetNamespace("MAPI")
/ Namespace.Logon("ProfileName")
, run your code, then close Outlook by calling Application.Quit
, then repeat the loop for the next profile. Hardly efficient, as each profile might require the user to log in.
If Outlook is already running, calling Namespace.Logon
will do nothing - you would need to close it first before starting in a different profile.
On the Extended MAPI level (C++ or Delphi only), you can do that without starting Outlook or initializing a MAPI session using the IProfAdmin interface. You can get a list of profiles using IProfAdmin::GetProfileTable
, open each profile by calling IProfAdmin::AdminServices
, find all MSPST
or MSUPST
services and delete them using IMsgServiceAdmin::DeleteMsgService
. You can play with these interfaces in OutlookSpy (I am its author - click IProfAdmin button).
If Extended MAPI in C++ or Delphi are not an option, you can use the ProfMan library (I am also its author) - it wraps the profile management interfaces and can be used from any language, including PS. You can modify the example at the ProfMan site to delete the PST services.
Upvotes: 2