How Can I Get a List of All the .PST Files on a Computer?

How Can I Get a List of All the .PST Files on a Computer?

  • Comments 7
  • Likes

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I get a list of all the .pst files on a computer - along with their size and their location - and then save that information to a comma-separated values file?

-- DC

SpacerHey, Scripting Guy! AnswerScript Center

Hey, DC. If you hang around baseball fields long enough sooner or later you’ll hear someone use the expression “tailor-made.” Typically what they are referring to is not some player’s uniform but a ground ball that is doomed to be turned into a double play. Something that’s tailor-made can’t be anything but a double-play; for better or worse (depending on the team you’re rooting for) the grounder was perfectly designed to be turned into two quick outs.

Why do we mention this? (We mean besides the fact that at least one of the Scripting Guys spends far more time thinking about baseball than he does thinking about scripting.) Well, it turns out that this question is tailor-made for WMI: not only can WMI search any entire file system for any files that have a .pst file extension, but WMI can also retrieve folder information and file size as well. Tailor-made!

Just to prove it, here’s a script that retrieves information about all the .pst files on a computer. Note that this particular script displays information to the screen; we’ll show you how to save this data to a comma-separated values (CSV) file a little later on:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_DataFile Where Extension = 'pst'")

For Each objFile in colFiles
    Wscript.Echo objFile.Drive & objFile.Path
    Wscript.Echo objFile.FileName & "." & objFile.Extension
    Wscript.Echo objFile.FileSize
    Wscript.Echo
Next

Just a few things to point out here. After connecting to the WMI service we use ExecQuery to return a collection of all the files that have an Extension of pst. Note that we don’t include the period in the file extension: it’s pst rather than the .pst. That’s just the way WMI does things.

After retrieving this collection we create a For Each loop and echo the desired property values. This is a little bit tricky because of the way WMI stores naming information for a file. For example, suppose we have this file:

c:\documents and settings\kmyer\local settings\application data\microsoft\outlook\archive.pst

The naming information is stored like this in WMI:

WMI Property

Value

Drive

C:

Path

\documents and settings\kenmyer\local settings\application data\microsoft\outlook\

FileName

archive

Extension

pst

What does this mean? Well, it means that to display folder and files names the way we’re used to seeing them we have to combine these property values before echoing them to the screen. To echo the folder name we combine the Drive and Path properties, like so:

Wscript.Echo objFile.Drive & objFile.Path

To display the file name we combine the FileName and Extension properties:

Wscript.Echo objFile.FileName & "." & objFile.Extension

Note. Yes, we could have done this instead:

Wscript.Echo objFile.FileName & ".pst"

We took a more generic approach simply to show you how to construct file names in WMI.

To display the file size we - oh, well, we just echo the value of the FileSize property. Hurray for FileSize, huh?

Now, what about saving this data to a CSV file? We’ll show you a script that does that; if you need more information about saving data to a CSV file take a look at this portion of the Microsoft Windows 2000 Scripting Guide.

Here’s a script that retrieves information about .pst files and then saves that data to C:\Scripts\PST_Data.txt:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_DataFile Where Extension = 'pst'")

If colFiles.Count = 0 Then
    Wscript.Quit
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.CreateTextFile("C:\Scripts\PST_Data.txt")

For Each objFile in colFiles
    objTextFile.Write(objFile.Drive & objFile.Path & ",")
    objTextFile.Write(objFile.FileName & "." & objFile.Extension & ",")
    objTextFile.Write(objFile.FileSize & vbCrLf)
Next

objTextFile.Close

Notice how we write to the text file. We begin by writing the folder information followed by a comma. That gives us this:

c:\documents and settings\kmyer\local settings\application data\microsoft\outlook\,

Next we add the file name, followed by another comma. Our text file will thus look like this:

c:\documents and settings\kmyer\local settings\application data\microsoft\outlook\,archive.pst,

Finally we tack on the file size followed by a carriage-return linefeed (equivalent to pressing ENTER on the keyboard). We use the carriage return linefeed because we’re done with the first .pst file and we want information for the next .pst file to be shown on a separate line. Our text file now looks like this:

c:\documents and settings\kmyer\local settings\application data\microsoft\outlook\,archive.pst,68741

What’d we tell you: tailor-made.

Note that - depending on the size of your file system - this script might take 30 seconds or so to complete. If you know that your .pst files are likely to be found only on a particular drive or in a particular folder you might be able to speed that up a bit by modifying your WQL query. For example, this query searches only drive C: for .pst files:

Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_DataFile Where Extension = 'pst' AND Drive = 'C:'")

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Thanks will this function as a logon script and if it will how can it be run in the background so the users dont have to wait for completion. I also assume writing to the CSV file can be modified to write to a network location. Also can it pull a machine name or IP for referencing?

    Thanks

  • How can I change the default path via script? No one seems to have a simple script for this, is it really that difficult to do?

  • You can also replace:  objTextFile.Write(objFile.FileSize & vbCrLf)

    with...

    objTextFile.Write "Size: " & Round(objFile.FileSize/1024^2) & "MB" & vbCrLf

    This will display the PST in MB size and round it out for easier reading

  • Hello, I see that this is a really old post so I don't know if anyone will respond.  The script you provided is great and I plan to deploy it on our networks asap so we can better manage users' pst files (a real source of IT headaches).  I am not very well versed in wmi queries and was wondering if there was a way to add a couple AND conditions to the query in this script.

    I would like the script to run like it does only add the AND conditions of lastmodified now-14 days AND size>5GB.  If any of the located pst files fit this criteria, continue building the csv file, if not, exit the script.  This would allow me to only look on drives for the csv file and not have to open every one of them.  If I see the file, there is work to be done, of not, move on.

    Any assistance with this would be tremendously appreciated.

    Thank you,

    Chris

  • Thanks for the script. For some reason it doesn't seem to return any Outlook 2007 pst files on my Win 7 Pro PC, yet my normal windows search finds them. Do you know of any reason why these are not being returned by the query?

    Works perfectly on a Win XP Pro PC with Outlook 2003.

  • So this is getting all the computers from my list and searching through them but before it can it gets access denied to each machine, where would be the best place to insert credentials?

  • What if I need to find every pst on netapp storage using unc?