VBScript: Find and Copy PST Files Using WMI

This script scans the local machine for all PST files using a WMI query against `CIM_Datafile`, then copies whatever it finds to a destination folder you specify at runtime. It's useful when you need to round up Outlook data files before a migration, decommission, or backup sweep — without knowing where users may have stashed them.

How It Works

Disconnect Mapped Drives

Before doing anything else, the script enumerates and removes all currently mapped network drives. This clears the deck so the WMI file search stays focused on local storage and doesn’t chase network paths.

Prompt for a Destination

An InputBox loop asks for a destination folder path and validates it with FileSystemObject.FolderExists. It won’t proceed until you give it a path that actually exists — no silent failures here.

WMI Full-Disk File Enumeration

The core search uses WMI to query CIM_Datafile with no WHERE clause, then filters in VBScript for files where the extension matches pst (case-insensitive). This is a brute-force approach — it pulls every file record WMI knows about and checks each one. Slow, but thorough.

Copy on Match

When a PST is found, it’s copied to the destination using FSO.CopyFile with the Donotoverwrite flag set to False — meaning it will not overwrite an existing file at the destination. The script echoes progress to the console for each file found and each copy attempted.

Cleanup

All object references are explicitly set to Nothing at the end. Standard hygiene.

Usage

Run the script directly with cscript from a command prompt — using wscript will spawn a message box for every single file found, which gets painful fast.

cscript find_pst.vbsCode language: DOS .bat (dos)

Permissions required:

    • Local Administrator on the machine being scanned (WMI CIM_Datafile queries require it)
    • Write access to the destination folder

Dependencies:

    • Windows Script Host (built into Windows)
    • WMI service running on the local machine

At runtime:

    • You’ll be prompted for a destination path (e.g., d:mappeddrivesubfolder)
    • The path must already exist — the script won’t create it

Redirect output to a log file if you want a record:

cscript find_pst.vbs > pst_scan.logCode language: DOS .bat (dos)

Caveats

  • Speed: Querying CIM_Datafile with no filter is genuinely slow on machines with large file systems. Expect it to run for several minutes on a typical workstation. There’s no progress indicator beyond per-file echo output.
  • On Error Resume Next is used twice: Errors are silently swallowed throughout. If a file copy fails (locked PST, permissions issue, path too long), the script keeps going without telling you. Check your destination folder count against the echo output to spot mismatches.
  • Locked PSTs: Outlook keeps its PST files open while running. Any PST currently in use will likely fail to copy silently due to the error handling above. Close Outlook before running.
  • No WHERE clause on the WMI query: This is intentional for completeness, but it means the script works harder than it needs to. On systems with hundreds of thousands of files, memory and time costs add up.
  • Filename collisions: If two PSTs in different directories share the same filename, only the first one lands at the destination — Donotoverwrite = False means the second copy attempt is skipped without warning.
  • Drive disconnection at startup: The script removes all mapped network drives unconditionally. If anything else on the machine depends on those mappings during the run, that’s going to be a problem.
  • Local machine only: strComputer = "." hardcodes this to run locally. It’s not built for remote execution as-is.

Full Script

Option Explicit
On Error Resume Next
Set objNetwork = CreateObject("Wscript.Network")
Set colDrives = objNetwork.EnumNetworkDrives
For i = 0 to colDrives.Count-1 Step 2
    objNetwork.RemoveNetworkDrive colDrives.Item(i)
Next
Dim strComputer, strExtension, strDestination, strFolderExists
Dim objWMIService, colFiles, objFile, objFolder, objFolderItem, objShell, objFSO
Const Donotoverwrite = False
On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
strComputer = "."
strExtension = "pst"
strFolderExists = "False"
While strFolderExists <> "True"
    strDestination = InputBox("Enter the destination without the trailing backslash " & vbCr _
    & "i.e. d:mappeddrivesubfolder", "Destination", "d:mappeddrivesubfolder")
    If objFSO.FolderExists(strDestination) Then
        Set objFolder = objFSO.GetFolder(strDestination)
        strFolderExists = "True"
    Else
        WScript.Echo "Folder " & strDestination & " does not exist."
        strFolderExists = "False"
    End If
Wend
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")
Set colFiles = objWMIService.ExecQuery("Select * from CIM_Datafile")
For Each objFile in colFiles
If LCase(objFile.Extension) = LCase(strExtension) Then
    WScript.Echo " Found " & objFile.Name
    WScript.Echo " Attempting to copy " & objFile.Name & " to " & strDestination & _
        "" & " ..."
    objFSO.CopyFile objFile.Name , strDestination & "", Donotoverwrite
    WScript.Echo " Finished " & objFile.Name & "!"
End If
Next
Set strComputer = Nothing
Set strExtension = Nothing
Set strDestination = Nothing
Set strFolderExists = Nothing
Set objWMIService = Nothing
Set colFiles = Nothing
Set objFile = Nothing
Set objFolder = Nothing
Set objFolderItem = Nothing
Set objShell = Nothing
Set objFSO = NothingCode language: VBScript (vbscript)