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
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.
No. If it is enabled as a log on or log off script, only the admin can disable it. Sorry.
Hi.
How do I remove only one pst?
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
Will this leave OST files untouched or are those removed as well?
it leaves the ost files untouched - it only applies to pst files.
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
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.)
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
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
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
sorry about that - Add Quit (or wscript.quit) after (or in place of) the msgbox.
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?
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
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?
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.