VBScript to find and report pst files

Is there an existing script (vbscript preferably) that can be deployed to a few dozen computers that would:

1. Check for any .pst files on the computer
2. Report the computer name & username appending to an excel sheet on a server share, so I could analyze the PC names and contact these users to....stop using PST files!.

I'll be implementing group policy to stop this, but I believe some users may still be using pst files which is why I need a good script that would be able to run on different computers.

I don't need to import a list of computers to scan, just need the script to work on one PC, and I can deploy it to more PC's if needed.

garryshapeAsked:
Who is Participating?
 
Justin EllenbeckerConnect With a Mentor IT DirectorCommented:
Yes it makes sense, what I would do personally is write the files out to a network share and have the computer name as the filename.  Here is the script updated to do that just replace the data and make sure whoever runs the script has access to that path on the network.
Set oFS = CreateObject("Scripting.FileSystemObject")
Set wshNetwork = WScript.CreateObject( "WScript.Network" )
strComputerName = wshNetwork.ComputerName
outfile = "\\servername\share\" & strComputerName & ".txt"
Set oTF = oFS.CreateTextFile (outfile)
Search("C:\")
oTF.Close

Function Search(path)
	On Error Resume Next
	Set oFolder = oFS.GetFolder(path)
	For Each folder In oFolder.SubFolders
		Search(folder.path)
	Next
	For Each file In oFolder.Files
    	If Right(LCase(File.name),4) = ".pst" Then
    		oTF.writeline(File.Path)
    		oTF.WriteLine(File.DateLastModified)
    		oTF.WriteLine(File.Size / 1024 & "KB" & VbCrLf)
    	End If
	Next
End Function

Open in new window

0
 
t0t0Commented:
The following batch commands would return the info you're looking for:

   @echo off
   echo %computername%
   echo %username%
   attrib /s c:\*.pst
   
0
 
t0t0Connect With a Mentor Commented:
To get the output to a file (text file) you could do:

   @echo off
   (
      echo %computername%
      echo %username%
      attrib /s c:\*.pst
   )>>file


NOTE: replace the word FILE with the full path and filename of your output file.

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
t0t0Commented:
Or you can run the (first) code (above) like this:


    BATCHNAME >>UNCfilename


where BATCHNAME is the name of your batch file and UNCfilename is the UNC path and filename of the file you want the data appended to.
0
 
Justin EllenbeckerIT DirectorCommented:
Would a text file with each PST location be sufficient or does it need to be an excel sheet?  If a text file is OK you can do this, it will create a text file in the root of C and each line will be a different PST path.
On Error Resume Next
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oTF = oFS.CreateTextFile ("C:\PST Files.txt")
Search("C:\Documents and Settings\")

Function Search(path)
	Set oFolder = oFS.GetFolder(path)
	For Each folder In oFolder.SubFolders
    	WScript.Echo folder
    	Search(folder.path)
	Next
	For Each file In oFolder.Files
    	If InStr(LCase(File.path),".pst") Then
    		oTF.writeline(File.Path)
    	End If
	Next
End Function

Open in new window

0
 
Justin EllenbeckerIT DirectorCommented:
Take my echo folder out of there though or it will take forever to run I was doing that to make sure it was working right.

Remove Line 9
0
 
Justin EllenbeckerIT DirectorCommented:
Also you can change line 4 to just C: but you might run into permissions errors like I did with the MSO cache folder so I started in Documents and Settings.
0
 
Justin EllenbeckerIT DirectorCommented:
OK one more and this may not make a difference to you but after running it on my machine it returned a .pst.temp file because of the InStr.  So I changed it to only look at the last 4 chars for .pst which give a little better results. It also only looks at the file name not the entire path for the .pst which should help it run a little faster, and I took out that echo.
On Error Resume Next
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oTF = oFS.CreateTextFile ("C:\PST Files.txt")
Search("C:\Documents and Settings\")

Function Search(path)
	Set oFolder = oFS.GetFolder(path)
	For Each folder In oFolder.SubFolders
		Search(folder.path)
	Next
	For Each file In oFolder.Files
    	If Right(LCase(File.name),4) = ".pst" Then
    		oTF.writeline(File.Path)
    	End If
	Next
End Function

Open in new window

0
 
garryshapeAuthor Commented:
Thanks for that let me give that a test
0
 
Justin EllenbeckerIT DirectorCommented:
OK did still more clean up and came up with this the final version that can be run in the root of C: and works every time with no errors, the only downside is if you cannot goto the folder and double click on it to see it then this script will over look it as well. So if someone put it in a folder that you do not have access to then it will be missed.  I had to move the On Error into the function in order for it to process the access denied properly.
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oTF = oFS.CreateTextFile ("C:\PST Files.txt")
Search("C:\")

Function Search(path)
	On Error Resume Next
	Set oFolder = oFS.GetFolder(path)
	For Each folder In oFolder.SubFolders
		Search(folder.path)
	Next
	For Each file In oFolder.Files
    	If Right(LCase(File.name),4) = ".pst" Then
    		oTF.writeline(File.Path)
    	End If
	Next
End Function

Open in new window

0
 
Steve KnightConnect With a Mentor IT ConsultancyCommented:
Here is short one I have on my site which basically does what you ask for... slightly amended below for different paths and to include the computername though I know you already have a couple of solutions there already:

http://scripts.dragon-it.co.uk/links/batch-search-pst-files

@Echo off
set output="\\server\share\output.csv"
for /f "tokens=*" %%a in ('dir /b /s /a-d "c:\*.pst"') do (echo %date%,%time%,%computername%,%%~na,%%~pa,%%~za)>>%output%
start excel %output%

That makes a CSV file \\server\share\output.csv which then loads in Excel... remove the START command at the end to stop that of course.

Steve
0
 
garryshapeAuthor Commented:
I'm still trying to get this to work to where I can easily analyze all the data.

It seems that nearly all the PC's have a pst file (i.e. archive.pst) but some are small in size. I know I can echo the file size in bytes, but am wondering if it's possible to echo the Last Modifed date as well.
0
 
Justin EllenbeckerIT DirectorCommented:
I added the line below that will write the date last modified below the archive path.
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oTF = oFS.CreateTextFile ("C:\PST Files.txt")
Search("C:\")

Function Search(path)
	On Error Resume Next
	Set oFolder = oFS.GetFolder(path)
	For Each folder In oFolder.SubFolders
		Search(folder.path)
	Next
	For Each file In oFolder.Files
    	If Right(LCase(File.name),4) = ".pst" Then
    		oTF.writeline(File.Path)
    		oTF.WriteLine(File.DateLastModified)
    	End If
	Next
End Function

Open in new window

0
 
Justin EllenbeckerIT DirectorCommented:
oTF.WriteLine(File.Size)

If you add that line either before or after the DateLastModified line it will put the size in as well.  That will give you the answer in bytes.
0
 
garryshapeAuthor Commented:
Thanks for the modification. With the batch file I can modify it to save the text or CSV file with the %ComputerName%_%UserName% as the file name. However with the VBS it appears I can't.

At this point, I'm wondering if, with the vbscript, I can either A.) include the computer name and logged on username as the File Name or B.) append all output of the scripts that runs on about 100 computers to the same exact file and just add the Computer Name in the path, because without either I won't be able to identify which computers to manually go after. Does that make sense
0
 
Justin EllenbeckerIT DirectorCommented:
If you need the username in the file this will create the file in the following format:

computername.username
Set oFS = CreateObject("Scripting.FileSystemObject")
Set wshNetwork = WScript.CreateObject( "WScript.Network" )
strComputerName = wshNetwork.ComputerName
strUserName = wshNetwork.UserName
outfile = "\\servername\share\" & strComputerName & "." & strUserName & ".txt"
Set oTF = oFS.CreateTextFile (outfile)
Search("C:\")
oTF.Close

Function Search(path)
	On Error Resume Next
	Set oFolder = oFS.GetFolder(path)
	For Each folder In oFolder.SubFolders
		Search(folder.path)
	Next
	For Each file In oFolder.Files
    	If Right(LCase(File.name),4) = ".pst" Then
    		oTF.writeline(File.Path)
    		oTF.WriteLine(File.DateLastModified)
    		oTF.WriteLine(File.Size / 1024 & "KB" & VbCrLf)
    	End If
	Next
End Function

Open in new window

0
 
Justin EllenbeckerIT DirectorCommented:
I also modified it to automatically divide the size of the file by 1024 add KB after it and create a blank line so they are not all smashed together.
0
 
Justin EllenbeckerIT DirectorCommented:
outfile = is where you need to replace what is inside of quotations make sure to leave the trailing \ after the share.
0
 
garryshapeAuthor Commented:
Thanks for all your help all the solutions worked but Strife's was more detailed and searches/reports in a better format for analyzing
0
All Courses

From novice to tech pro — start learning today.