Create an Excel spreadsheet of running processes (VBScript)

This code can be found in Chapter 1 of Windows Server Cookbook

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the VBScript code from this site.

' From the book "Windows Server Cookbook" by Robbie Allen
' ISBN: 0-596-00633-0

' ------ SCRIPT CONFIGURATION ------
strComputer = "."
strExcelPath = "d:\procs.xls"
' ------ END CONFIGURATION ---------
On Error Resume Next
set objExcel = CreateObject("Excel.Application")
if Err.Number <> 0 then
	Wscript.Echo "Excel application not installed."
	Wscript.Quit
end if
On Error GoTo 0
' Create a new workbook.
objExcel.Workbooks.Add
' Bind to worksheet.
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
objSheet.Name = "Processes"
' Populate spreadsheet cells with user attributes.
objSheet.Cells(1, 1).Value = "Process Name"
objSheet.Cells(1, 2).Value = "Command Line"
objSheet.Cells(1, 3).Value = "PID"
objSheet.Cells(1, 4).Value = "Owner"
objSheet.Range("A1:D1").Font.Bold = True
' Query process information
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
intProcessCount = 1
for each objProcess in objWMI.InstancesOf("Win32_Process")
	' For each process, write the name, command-line options and process ID
	' to the spreadsheet
	intProcessCount = intProcessCount + 1
	objSheet.Cells(intProcessCount,1).Value = objProcess.Name
	objSheet.Cells(intProcessCount,2).Value = ObjProcess.CommandLine
	objSheet.Cells(intProcessCount,3).Value = ObjProcess.ProcessID
	objProcess.GetOwner strUser,strDomain
	objSheet.Cells(intProcessCount,4).Value = strDomain & "\" & strUser
next
' This formats the columns
objExcel.Columns(1).ColumnWidth = 20
objExcel.Columns(2).ColumnWidth = 50
objExcel.Columns(3).ColumnWidth = 5
objExcel.Columns(4).ColumnWidth = 30
' Save the spreadsheet, close the workbook and exit.
objExcel.ActiveWorkbook.SaveAs strExcelPath
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
WScript.Echo "Done"

This code has been viewed 15971 times.

New from the creators of TechTasks.com: StatSheet.com