• Outlook User
  • New Outlook app
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
  • Developer
  • Microsoft 365 Admin
    • Common Problems
    • Microsoft 365
    • Outlook BCM
    • Utilities & Addins

Script to remove all PST files from a profile

Slipstick Systems

› Exchange Server › Script to remove all PST files from a profile

Last reviewed on December 4, 2018     50 Comments

An administrator is looking for an easy way to remove PST files from his user's profiles after configuring a policy to prevent the creation of PST files.

We recently implemented an email retention policy of 6 months for company email. We are enforcing the policy on Exchange 2010. Getting rid of the PST’s has been a headache. We disabled the creation of new PST’s and disabled PST growth using group policy. We asked users to close their old archives in Outlook 2010, and deleted the associated PST when they did, but many Outlook users are dragging their feet. We could delete the PSTs when Outlook is closed, but that will cause Outlook to error the next time it’s launched (and then the phone rings). We’re looking for a way to automatically detach PSTs, that is invisible to the end-user. We suspect this will have to be done programmatically.

Correct, removing the PST files can be done programmatically, but it won't be difficult. You can use the following VBScript in a logon script to remove PST files from the default profile on the computer (if Outlook is closed). If the script runs when Outlook is open, it will remove the PST files from the profile currently in use.

To use, copy the code below and paste it into Notepad. Save the file with a vbs extension. Double-click on the file to run it, or use it with a logon script to run when the user's log on their computer.

On Error Resume Next
Dim objOutlook 'As Outlook.Application
Dim Stores     'As Outlook.Stores
Dim objFolder  'As Outlook.Folder
Dim i          'As Integer

Set objOutlook = CreateObject("Outlook.Application")
Set Stores = objOutlook.Session.Stores
 

For i =  Stores.Count to 0 step -1

If Stores(i).ExchangeStoreType = 3 Then
   Set objFolder = Stores(i).GetRootFolder
   objOutlook.Session.RemoveStore objFolder
 Else
End If

Next
                    

Remove all PST files except SharePoint Lists

If you are using SharePoint Lists linked to Outlook, the SharePoint data is in a PST file and will be removed using the script above. Of course, if you don't allow the creation of PST files, you can't link to SharePoint libraries anyway...

If you want to remove all PST files from the profile, except for the SharePoint List data file, use an If statement:

    If Stores(i).ExchangeStoreType = 3 Then
      If Stores(i).DisplayName <> "SharePoint Lists" then
       Set objFolder = Stores(i).GetRootFolder
       objOutlook.Session.RemoveStore objFolder
      End if
    Else 

More Information

ExchangeStoreType property

Script to remove all PST files from a profile was last modified: December 4th, 2018 by Diane Poremsky

Related Posts:

  • Rename Account in the Navigation Pane
  • Moving Outlook .pst files to a new computer
  • When you have an Exchange Server account in Small Business Server 2003
    Exchange Account Gets Set as the Default Account
  • open file location from settings
    Where is Outlook's PST file?

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Subscribe
Notify of
50 Comments
newest
oldest most voted
Inline Feedbacks
View all comments

Tolga
February 19, 2021 12:37 am

Hello, is there a way to block this kind of script as user :)

I guess our company also added a script to close all .PST files, at every Outlook login it closes all open .PST files. I created a script to batch open all .PST files but i want to disable this script at the start point.

0
-1
Reply
Diane Poremsky
Author
Reply to  Tolga
February 19, 2021 9:59 am

No. If it is enabled as a log on or log off script, only the admin can disable it. Sorry.

0
0
Reply
YongGun
June 12, 2019 11:55 pm

Hi.
How do I remove only one pst?

0
0
Reply
Diane Poremsky
Author
Reply to  YongGun
June 13, 2019 1:00 am

if you know the name, use
If Stores(i).DisplayName = "pst name" then

if you know the position, you can use the index # - something like this
Set objFolder = Stores(5).GetRootFolder
objOutlook.Session.RemoveStore objFolder

0
0
Reply
Christoph
August 2, 2017 1:43 pm

Will this leave OST files untouched or are those removed as well?

0
0
Reply
Diane Poremsky
Author
Reply to  Christoph
August 3, 2017 12:53 am

it leaves the ost files untouched - it only applies to pst files.

0
0
Reply
Ashis Prasad
January 10, 2017 11:21 pm

Hi,

The scripts worked perfectly fine. Thanks for that.

We have users having scripts in C: drive as well as in D: drive.

Now, I want to detach psts (from Outlook) only for psts in D: drive. Please help me this. Will be highly thankful.

Thanks,
Ashis

0
0
Reply
Diane Poremsky
Author
Reply to  Ashis Prasad
January 11, 2017 12:43 am

stores(i).filepath will get the file path - use something like
left(1,stores(i).filepath, "D") = 1 then (not sure if that syntax is correct for vbs.)

0
0
Reply
JR
August 20, 2015 5:57 pm

Ok that worked. I had to modify the REG Key based on my Version of Outlook (2010) but I noticed that it just quits if there is a profile and doesn't proceed to remove the PST. What do I have wrong here?

On Error Resume Next
set oshell = createobject("wscript.shell")
skey = ""
skey = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\DefaultProfile"
on error resume next
sprofile = oshell.regread(skey)
if err then
msgbox "Unable to find a mail profile..."
end if
on error goto 0
Wscript.quit

Dim objOutlook 'As Outlook.Application
Dim Stores 'As Outlook.Stores
Dim objFolder 'As Outlook.Folder
Dim i 'As Integer

Set objOutlook = CreateObject("Outlook.Application")
Set Stores = objOutlook.Session.Stores

For i = Stores.Count to 0 step -1

If Stores(i).ExchangeStoreType = 3 Then
Set objFolder = Stores(i).GetRootFolder
objOutlook.Session.RemoveStore objFolder
Else
End If

Next

0
0
Reply
Diane Poremsky
Author
Reply to  JR
August 20, 2015 8:24 pm

This checks then quits... you need the quit within the if/end if
if err then
msgbox "Unable to find a mail profile..."
end if
on error goto 0
Wscript.quit

Should be
if err then
msgbox "Unable to find a mail profile..."
Wscript.quit
end if
on error goto 0

0
0
Reply
JR
August 20, 2015 4:37 pm

I spoke too soon. After I close the 2nd Pop up, it still prompts me to create a New Outlook Profile. Here is what I have as the script:

On Error Resume Next
set oshell = createobject("wscript.shell")
skey = ""
skey = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\16.0\Outlook\Profiles\Outlook"
on error resume next
sprofile = oshell.regread(skey)
if err then
msgbox "Unable to find a mail profile..."
end if
on error goto 0
msgbox sprofile
Dim objOutlook 'As Outlook.Application
Dim Stores 'As Outlook.Stores
Dim objFolder 'As Outlook.Folder
Dim i 'As Integer

Set objOutlook = CreateObject("Outlook.Application")
Set Stores = objOutlook.Session.Stores

For i = Stores.Count to 0 step -1

If Stores(i).ExchangeStoreType = 3 Then
Set objFolder = Stores(i).GetRootFolder
objOutlook.Session.RemoveStore objFolder
Else
End If

Next

0
0
Reply
Diane Poremsky
Author
Reply to  JR
August 20, 2015 4:43 pm

sorry about that - Add Quit (or wscript.quit) after (or in place of) the msgbox.

0
0
Reply
JR
August 20, 2015 10:21 am

We recently deployed this scrip to all of our users and it seems to do the trick however. If Outlook is installed on a machine with no Profile configured it launces the Outlook client and tries to get it to configure a profile. Is there something I can add to the scrip to tell it to only run when a profile is configured?

0
0
Reply
Diane Poremsky
Author
Reply to  JR
August 20, 2015 4:21 pm

If the profiles will use the same name (default is Outlook) and all use the same version of Outlook, this will work - if the profile names or version will change, it's a little more complicated to check for subfolders (and i haven't yet gotten it to work).
set oshell = createobject("wscript.shell")
skey = ""
skey = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\16.0\Outlook\Profiles\Outlook"
on error resume next
sprofile = oshell.regread(skey)
if err then
msgbox "Unable to find a mail profile..."
end if
on error goto 0
msgbox sprofile

0
0
Reply
JR
Reply to  JR
August 20, 2015 4:32 pm

Thanks Diane this works great! Only one thing I get a blank message box after the "Unable to find a Mail profile" box. Can I remove this or maybe add another message to it?

0
0
Reply
Daniel Vasconcelos Ferreira
May 3, 2015 6:15 pm

Diane,
You're awesome.
It's work perfect here..but, look if you can help me with other things.

I need one script to add other pst to profile outlook..that stay on network..
like \\10.10.10.1\pst\%Username%
this pst will be default.

has one code to do this?
sorry for my english, i'm brazilian.

cya.

0
0
Reply

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 30 Issue 25

Subscribe to Exchange Messaging Outlook






Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • Jetpack plugin with Stats module needs to be enabled.
  • Open Outlook Templates using PowerShell
  • Count and List Folders in Classic Outlook
  • Google Workspace and Outlook with POP Mail
  • Import EML Files into New Outlook
  • Opening PST files in New Outlook
  • New Outlook: Show To, CC, BCC in Replies
  • Insert Word Document into Email using VBA
  • Delete Empty Folders using PowerShell
  • Warn Before Deleting a Contact
  • Classic Outlook is NOT Going Away in 2026
Ajax spinner

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in classic Outlook (Windows).

For new Outlook for Windows: Fixes or workarounds for recent issues in new Outlook for Windows .

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Outlook.com Recent issues: Fixes or workarounds for recent issues on Outlook.com

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Outlook (new) Feedback. Use this for feedback and suggestions for Outlook (new).

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




New Outlook Articles

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Google Workspace and Outlook with POP Mail

Import EML Files into New Outlook

Opening PST files in New Outlook

New Outlook: Show To, CC, BCC in Replies

Insert Word Document into Email using VBA

Delete Empty Folders using PowerShell

Warn Before Deleting a Contact

Classic Outlook is NOT Going Away in 2026

Newest Code Samples

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Insert Word Document into Email using VBA

Warn Before Deleting a Contact

Use PowerShell to Delete Attachments

Remove RE:, FWD:, and Other Prefixes from Subject Line

Change the Mailing Address Using PowerShell

Categorize @Mentioned Messages

Send an Email When You Open Outlook

Delete Old Calendar Events using VBA

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Diane Poremsky [Outlook MVP]

Make a donation

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Submit Outlook Feature Requests

Slipstick Support Services

Buy Microsoft 365 Office Software and Services

Visit Slipstick Forums.

What's New at Slipstick.com

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2025 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

:wpds_smile::wpds_grin::wpds_wink::wpds_mrgreen::wpds_neutral::wpds_twisted::wpds_arrow::wpds_shock::wpds_unamused::wpds_cool::wpds_evil::wpds_oops::wpds_razz::wpds_roll::wpds_cry::wpds_eek::wpds_lol::wpds_mad::wpds_sad::wpds_exclamation::wpds_question::wpds_idea::wpds_hmm::wpds_beg::wpds_whew::wpds_chuckle::wpds_silly::wpds_envy::wpds_shutmouth:
wpDiscuz

Sign up for Exchange Messaging Outlook

Our weekly Outlook & Exchange newsletter (bi-weekly during the summer)






Please note: If you subscribed to Exchange Messaging Outlook before August 2019, please re-subscribe.

Never see this message again.

You are going to send email to

Move Comment