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
Tolga says
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.
Diane Poremsky says
No. If it is enabled as a log on or log off script, only the admin can disable it. Sorry.
YongGun says
Hi.
How do I remove only one pst?
Diane Poremsky says
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
Christoph says
Will this leave OST files untouched or are those removed as well?
Diane Poremsky says
it leaves the ost files untouched - it only applies to pst files.
Ashis Prasad says
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
Diane Poremsky says
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.)
JR says
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
Diane Poremsky says
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
JR says
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
Diane Poremsky says
sorry about that - Add Quit (or wscript.quit) after (or in place of) the msgbox.
JR says
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?
Diane Poremsky says
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
JR says
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?
Daniel Vasconcelos Ferreira says
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.
Bryan says
But that wouldn't remove those PSTs and from what I tested it doesn't throw an actual error ... it just prompts the user. You need to pass in the store's folder name but if you try and get it then you are prompted for the file location.
Bryan says
Diane,
Any thoughts on handling PSTs that have already been deleted or are unavailable for some reason? Anytime the script comes across one you get an error "The file #### cannot be found" and then are prompted to find the file. I've tried different properties / methods but it seems anytime you access that store it does this. To test I created a PST file, opened it, closed Outlook, and deleted the PST file.
Diane Poremsky says
Use on error goto error handling to jump over some lines when it encounters an error.
Nishat says
Hi Diane,
The script works fine in disconnecting the PST attached to the default Outlook profile, but it doesn't disconnect the PST which has been set as default to receive mail from the exchange server.
Account Settings --> Email Tab -> Change Folder --> Local PST is selected
Account Settings --> Data Files Tab --> same Local PST is set as default
Is there any way to disconnect this PST, as running your script gives an error -
"You cannot close the mailbox that contains your calendar, contacts, and inbox."
Please advise as we have 100s of users with this setup and we are looking to migrate them to Large mailbox solution.
Diane Poremsky says
You would need to set the exchange mailbox as default before closing the pst.
Nishat says
Can that be done programmatically? As we have too many users we would need to set the exchange mailbox as default via a script. Please suggest
Diane Poremsky says
As far as I know, no, but I'm checking on it to verify. You can do it using a prf and log on script.
Eran says
Hi,
following this Script,
I need help to Disconnect all PST files from outlook and then copy files to storage share.
important : the connected PST files are located locally (computers) and on storage share.
for example: User-A have archive.pst (locally) + archive.pst (Network share)
it's possible that we have 2 files with the same name but we need both of them. so it's makes conflict when we want to copy it.
So we also need a way to rename OR open new folder to every new file for every user.
on the first level , i planing to open new folder for every computer , like this: -->
Dim computer : computer = lcase (WshNetwork.ComputerName)
strDirectory = "\\storageShare\newPST\" &computer &"\"
i will be happy if someone have kind of this script or anyone can help me with this script.
thanks!
Diane Poremsky says
do you need to remove them or just close outlook so they can be copied?
Renaming isn't difficult - you need to use FSO.
something like this:
Set fso = CreateObject("Scripting.FileSystemObject")
Set oldName = fso.GetFile(file)
DateFormat = Format(date, "yyyy-mm-dd ")
newName = DateFormat & oldname.name
oldName.Name = newName
Mike Avila says
you are my hero! The first script does just what i need. I save all my emails by client. each pst is getting huge. business is good -- so i'm creating almost one hundred pst files. which outlook does not like. so i can close them now in one click when too many are open and makes outlook choke, then re-open them as needed. thank you so much!
Vladimir says
Thank you very much! Works great! Very useful to me! :)
leplager says
So what we have decided on is to use the script at logoff because of timing issues. Since we tear down profiles after logoff with our citrix environment the .pst file didn't exist at logon and would give an error. Because the sharepoint lists.pst file is there at logoff we have no errors and the sharepoint lists stay connected in the profile. Thank you again for all the help.
Diane Poremsky says
Thanks for the update!
leplager says
It looks like the code is not removing the Sharepoint List now which is great news. Unfortunately I am receiving an error that the sharepoint lists.pst cannot be found each time outlook is launched. Not sure where that is coming from but the sharepoint list still works in outlook. Would it be better to try a LIKE statement instead of ?
Diane Poremsky says
You can try it, or try using =
If Stores(i).ExchangeStoreType = 3 Then
If Stores(i).DisplayName = "SharePoint Lists" then
'do nothing
else
Set objFolder = Stores(i).GetRootFolder
objOutlook.Session.RemoveStore objFolder
End if
Else
leplager says
Thanks again for all your responses. I really appreciate the help. I'll give this a try and see what happens. I'm thinking this may be a timing issue since we are a citrix shop. When a user is logged off the profile on that server is removed and rebuilt fresh each time they launch something the first time until they are logged off all apps again.. So i'm thinking that the pst script is running and not allowing the profile to be built completely since I get the error when I sign in directly to a citrix server without launching outlook.
Diane Poremsky says
That could very well be the problem. If you figure it out, please let me know, it may help someone else in the future.
Leplager says
I'm pretty much experiencing the same thing. Any thoughts on killing wscript after it runs? I guess the only way to do this at this time would be run the script and let it remove everything and then have the users subscribe to any sharepoint lists they need again.
Diane Poremsky says
Try this in place of the do loop -
for i = Stores.Count to 0 step -1
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
End If
next
Diane Poremsky says
BTW, my SharePoint pst hasn't been removed since the first time. I thought it was because I added them back in this profile but I also copied the name from the pst properties and pasted it in the code, on the chance something (maybe the space) wasn't an exact match.
Leplager says
I'm sorry but yes the PST is being removed from the profile. We only need PST files other than sharepoint to be removed.
Diane Poremsky says
Ok... I'm getting mixed results. The first time it took the SharePoint pst and the subscriptions were removed from account settings. I added them back and the sharepoint pst stayed (and worked). The next time it didn't remove any psts. It looks like the weirdness is because the script doesn't quit - if i test it again when one is still in task manager, the second try fails. I'm using Win8.1/Outlook 2013 64bit (MSI). Based on the publish date, I probably tested it with Outlook 2010 64bit on either win7 or 8.
Leplager says
We did originally have a GPO setup to prevent users from adding a PST to their profile. That was removed from the GPO though. Most users now have outlook 2010 published to them. Also most users are using windows 7. We do not allow users to create new PST files though to try and eliminate litigation issues and we also have Mimecast email archiving in place and PST files are no longer needed. Really appreciate the help.
leplager says
I have created a logon script that uses the script with the Sharepoint code in it but my users are receiving an error in outlook. It states that Sharepoint Lists.pst cannot be found. What am I doing wrong with the script or is it missing something?
Diane Poremsky says
Is the SharePoint pst removed from their profile? Are you using the regkey to block pst files from profiles? I'll double check it again to make sure nothing changed due to an update. What version of Outlook and windows do you use?
leplager says
I am trying to use this in a logon script to remove PST files from the default profiles of all our domain users. When I use the remove all except sharepoint script users that have Sharepoint Lists attached to outlook are receiving errors that it cannot find the Sharepoint Lists.pst file. Is there something i'm doing wrong here?
brood says
Yes, there are multiple profiles on each computer. PST files also can be anywhere on the local hard drive.
Diane Poremsky says
The script doesn't look for unattached pst files, so it doesn't matter where they are on the hard drive. It only checks the default profile (or open profile) and drops the pst that are in that profile. If you want to remove pst from all profiles, you'll need to walk all profiles. You can use redemption/profman for this.
Brood says
I tested this new script and when I run it at an elevated cmd with administrator rights, cscript.exe /nologo script.vbs the outlook 2010 choose profile box appears.
What I am hoping to do is deploy this script with configuration manager to silently remove all .pst files except the sharepoint lists.
Diane Poremsky says
Is there more than one profile on the computer?
brood says
I tried a few things. Running it as is gives me the compilation error:expected 'End'.
Diane Poremsky says
See if this file works without error. https://www.slipstick.com/files/remove-pst.vbs.txt
brood says
I tried the second script and I am receiving Type Mismatch: 'stores'. I have a x64 bit machine and am looking to deploy this script to remove all .pst files except the sharepoint lists.
Diane Poremsky says
I assume you replaced the If... Else block in the first macro with the snippet that removes all but the sharepoint pst files?