PST Powershell scripts

Hello, folks.

My boss asked me to create several tasks for our annual tasks and one of them was PST affair, He wants to arrange all PSTs files to

He wants to arrange all PSTs files to a safe location that will allow to IT team manage them simply.

What did he ask for:?

1) To output PST path with PST name that is connected to the user to TXT file.

2) Disconnect the PST from the user, remotely.

3) To relocate to the PST to a specific location (safety volume).

4) To connect the PST again, but with the new source (location).

I have 3 PST files:

* PST 1 locates on C:\peronal1.pst
* PST 2 locates on C:\Program Files\My Outlook Data File(1).pst
* PST 3 locates on C:\Users\Meirp\Documents\Virtual Machines\New.pst

As you already noticed each PST located in different locations which make a lot of mess for IT guys to centralize and manage them.BTW, it is a real scenario(Even in your Organization…)

And here you can find the solutions I made and found for you.
Script number 1 is to export all PST names that connected to OUTLOOK to a TXT file:

$usr=[Environment]::UserName $dmn=[Environment]::UserDomainName $comp=[Environment]::MachineName $outlook =
New-Object -comObject Outlook.Application $final=$outlook.Session.Stores | where { ($_.FilePath -like '*.pst')} |
select FilePath $final >> c:\Pstlist.txt
$new = get-content c:\Pstlist.txt del c:\Pstlist.txt foreach($line in $new){ $line = $line.trim() '"'+$line+'"'>>c:\Pstlist.txt }

Outcome:

“FilePath”
“——–”
“C:\Program Files\My Outlook Data File(1).pst”
“C:\Personal1.pst”
“C:\Users\Meirp\Documents\Virtual Machines\New.pst”

Script number 2 will disconnect the PSTS from outlook, along with the first script data and output that to TXT: (it happens online)

$outlook = New-Object -comObject Outlook.Application $list = $outlook.Session.Stores Foreach($lis in $list){ if($lis.ExchangeStoreType -eq 3)
{ $namespace = $outlook.getnamespace("MAPI") $PSTRoot= $lis.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 }
 } Foreach($lis in $list){ if($lis.ExchangeStoreType -eq 3){ $namespace = $outlook.getnamespace("MAPI") $PSTRoot= $lis.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 }
 } Foreach($lis in $list){ if($lis.ExchangeStoreType -eq 3){ $namespace = $outlook.getnamespace("MAPI") $PSTRoot= $lis.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 } } Foreach($lis in $list){ if($lis.ExchangeStoreType -eq 3){ $namespace = $outlook.getnamespace("MAPI") $PSTRoot= $lis.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 }
 } Foreach($lis in $list){ if($lis.ExchangeStoreType -eq 3){ $namespace = $outlook.getnamespace("MAPI") $PSTRoot= $lis.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 }
} Foreach($lis in $list){ if($lis.ExchangeStoreType -eq 3){ $namespace = $outlook.getnamespace("MAPI") $PSTRoot= $lis.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 }
 } Foreach($lis in $list){ if($lis.ExchangeStoreType -eq 3){ $namespace = $outlook.getnamespace("MAPI") $PSTRoot= $lis.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 }
} Foreach($lis in $list){ if($lis.ExchangeStoreType -eq 3){ $namespace = $outlook.getnamespace("MAPI") $PSTRoot= $lis.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 }
 }

Script number 3 knows to read all PST file’s path from the first TXT and move them to the different location:

$list = Get-Content C:\Pstlist.txt $dest = "C:\Users\meirp\Documents\Outlook Files" foreach ($line in $list){ xcopy $line $dest /y {

Script number 4 is very interesting because it knows to connect again the PST after they had moved it to a different location.

Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null $outlook = new-object -comobject outlook.application $namespace = $outlook.GetNameSpace("MAPI") dir “C:\Users\meirp\Documents\Outlook Files\*.pst” | % { $namespace.AddStore($_.FullName) }

* Make sure that nobody else uses in those PST files.

1 Comment
  1. Kyle Schwartz 3 months ago
    Reply

    This is really cool. It works for Outlook 2013 but fails for Outlook 2016. Do you have a script that works with Outlook 2016? I get this error message when I run it :

    New-Object : Retrieving the com class factory component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 800800005

    At C:\users\bobmacbo\documents\Script1.ps1:4 char:22
    + $outlook = New-Object <<<< -comObject Outlook.Application
    + CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException
    + FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand

    You cannot call a method on a null-valued expression.
    At C:\users\bobmacbo\documents\Script1.ps1:11 char:19
    + $line = $line.trim <<<< ()
    + CategoryInfo : InvalidOperation: (trim:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Leave a Comment

Your email address will not be published.

In the news
Load More