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

 

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:'”)