Partager via


Scripting: Adding PST files to an outlook profile automatically

This script will allow you to add all PSTs in a txt file (full path).

 On error resume next
     Const ForReading = 1 
     Dim arrFileLines() 
     Dim objNetwork,ObjFSO,objFile,objNet,objOutlook
     
     i=0
     
     Set objnet = CreateObject("wscript.network")
     Set objFSO = CreateObject("Scripting.FileSystemObject") 
     Set objFile = objFSO.OpenTextFile("c:\users\marcdek\temp\"& objNet.Username & "-PSTOUTPUT.txt", ForReading) 
     Set objFSO = CreateObject("Scripting.FileSystemObject")
     Set objOutlook = CreateObject("Outlook.Application") 
  
     Do Until objFile.AtEndOfStream 
         Redim Preserve arrFileLines(i) 
         arrFileLines(i) = objFile.ReadLine 
                 i = i + 1 
     Loop 
     
     objFile.Close
     
     For Each strPath in arrFileLines 
          objOutlook.Session.Addstore strPath
     Next  

And if you want to add a selection to only add local PST files you use the following:

 On error resume next
     Const ForReading = 1 
     Dim arrFileLines() 
     Dim objNetwork,ObjFSO,objFile,objNet,objOutlook
     
     i=0
     
     Set objnet = CreateObject("wscript.network")
     Set objFSO = CreateObject("Scripting.FileSystemObject") 
     Set objFile = objFSO.OpenTextFile("c:\users\marcdek\temp\"& objNet.Username & "-PSTOUTPUT.txt", ForReading) 
     Set objFSO = CreateObject("Scripting.FileSystemObject")
     Set objOutlook = CreateObject("Outlook.Application") 
  
     Do Until objFile.AtEndOfStream 
         Redim Preserve arrFileLines(i) 
         arrFileLines(i) = objFile.ReadLine 
                 i = i + 1 
     Loop 
     
     objFile.Close
     
     For Each strPath in arrFileLines 
          Select Case True
             Case InStr(strPath,":\") > 0  
                 objOutlook.Session.Addstore strPath
             Case InStr(strPath,"\\") > 0  
             End Select
     Next  

Comments

  • Anonymous
    January 01, 2003
    Works great on Outlook 2013! Thanks! I'm looking for the remove command so I have one script to add and one to remove.

  • Anonymous
    January 15, 2013
    What code would be needed in order for the script to scan a particular file on the local machine to auto populate the data in the -PSTOUTPUT.txt so I could run the code on a rebuilt machine and auto add all the .pst files on the machine to the usser's outlook profile? Some of my clients have hundreds of pst files and adding them individually is a big time waster.

  • Anonymous
    January 15, 2013
    Hi DraakusM, You could use the following script to populate that list: blogs.technet.com/.../scripting-listing-pst-files-in-an-outlook-profile.aspx

  • Anonymous
    November 10, 2013
    Exactly what I was looking for (with over 100 PSTs... don't ask. :) ) One caveat for people... if one or more of them is corrupt, it can take a LONG time for it to come back from the script.  You might want to break up your list into 10 file chunks (or less) and just keep repopulating the file list. Thank you!

  • Anonymous
    September 15, 2014
    Where i put the txt file?

  • Anonymous
    September 15, 2014
    If my supposition is good... for exemple the username of the user is
    Bblaze the path Will be:
    C:usersmarcdektempbblaze-PSTOUTPUT.txt

  • Anonymous
    September 15, 2014
    C:usersbblazetempbblaze-PSTOUTPUT.txt

  • Anonymous
    April 10, 2015
    This doesn't seem to work at all. Has this been tested with outlook 2010 on Win 7?

  • Anonymous
    June 18, 2015
    I've used this script with limited success.

    I have a list of 87 PST files that I need to create - long story...

    Anyway, this script only seems to create between 70-75 PST file and then it doesn't create any more. Even if I break it down into chunks of say 40 & 47, it will create the first 40 ok and when I run the modified script to create the next batch of 47 it will only create approximately the first 30-35 and then it just ends with no error on screen.

    Is there a maximum amount of text that can be processed by the script?

    I've tried another similar script that has all the PST file names to create inside an array and I get a similar number (maybe up to 75 vs 70 with this exact script reading from a separate txt file. Again, if I break it down into similar chunks it's still the same and will only create 70-75 total and then stop executing.

    I'm going to try to shorten the path and filenames from the current

    C:UsersUSERIDAppDataLocalMicrosoftOutlookMYPSTABC.PST

    to something like

    C:TEST\MYPSTABC.PST

    and see if that makes any difference, but it's still going to cause me some problems in what I'm trying to achieve if I can get it working.

    --

    Some PST files that I'm creating have spaces in their names, but that's not the issue and those get created OK.

  • Anonymous
    June 18, 2015
    Has this been tested with outlook 2010 on Win 8?

  • Anonymous
    June 18, 2015
    In my case I've only been able to test with Outlook 2010 (32 bit) on Win7 (64 bit)

  • Anonymous
    June 18, 2015
    I shortened the path to create the PST files in C:TESTFOLDER in the hope that it would make some difference, but it didn't really make any as it only created the first 61 files in the list and then stopped.
    I then tried splitting the script into 2 batches and started from scratch again. When starting from 62 to 87 it created the 26 files perfectly, and then when I ran the 1-61 batch it only created the first 41 files to give me a total of 67 files instead of the 87 total that I have...

    There's nothing wrong in the file names because if I run them separately everything gets created - it just seems to be a problem with the total count of files it can create & open in Outlook via script.

  • Anonymous
    June 18, 2015
    I'm baffled...

    I just shortened my file list again to be as follows
    C:TESTFOLDERfile1.txt
    C:TESTFOLDERfile2.txt
    C:TESTFOLDERfile3.txt
    right down to C:TESTFOLDERfile87.txt

    --

    The script will create as far as C:TESTFOLDERfile68.txt and then it just looks like it completes as normal with no error and if I run it again it won't create any more...

    If I split the file into chunks it will still only create 68 files total...

  • Anonymous
    January 21, 2016
    We would like to create a blank pst file on our network server for each user in their home folder and have it automatically add the pst to their outlook account. Is there a way to accomplish this?