How to split PST file according to year.

Here’s a sample PowerShell script that can be used to split a PST file:

# Import the required assemblies
Add-Type -Assembly "Microsoft.Office.Interop.Outlook"

# Define the input PST file and the output PST file
$inputPST = "C:\path\to\input.pst"
$outputPST = "C:\path\to\output.pst"

# Open the input PST file
$pst = New-Object -ComObject Outlook.Application
$namespace = $pst.GetNameSpace("MAPI")
$inbox = $namespace.OpenSharedItem($inputPST)

# Define the criteria for splitting the PST file
# In this example, we are splitting by date and creating a new PST file for each year
$years = @(2015, 2016, 2017, 2018, 2019, 2020)
foreach ($year in $years) {
    # Create a search filter to find items within the specified year
    $filter = "[ReceivedTime] >= '$year-01-01 00:00:00' AND [ReceivedTime] < '$(($year+1))-01-01 00:00:00'"

    # Find the items that match the search filter
    $items = $inbox.items.Restrict($filter)

    # Create a new PST file for the current year
    $newPST = $namespace.AddStoreEx($outputPST.Replace(".pst", $year + ".pst"), "IPF.Note")

    # Move the items to the new PST file
    $items | % {$_.MoveTo($newPST.GetRootFolder())}
}

# Close the PST files
$inbox.Close()
$newPST.Close()

This script will open the input PST file and create a new PST file for each year specified in the $years array. It will then search the input PST file for items received within the specified year and move those items to the corresponding output PST file.

You can modify the search filter to split the PST file in different ways, such as by sender or subject. You can also adjust the output PST file names to suit your needs.

To run the above PowerShell code, you will need:

  1. A computer running a version of Windows that supports PowerShell. The code has been tested on PowerShell 5.1, but it should also work on newer versions.
  2. Microsoft Outlook installed on the computer. The code uses the Microsoft.Office.Interop.Outlook assembly to access the PST file, so you will need Outlook to be installed in order to use this assembly.
  3. The input PST file that you want to split. Make sure that the file exists and that you have the necessary permissions to access it.
  4. The output PST file(s) should not already exist. The code will create new PST files for each year specified in the $years array. Make sure that you have write permissions to the destination folder where the output PST files will be created.
  5. If you are running the script on a machine with Outlook installed, you will also need to make sure that Outlook is not running when you execute the script. This is because the script uses the Outlook COM object to access the PST file, and Outlook needs to be closed in order for the COM object to be available.

Leave a Comment

Your email address will not be published. Required fields are marked *