Script Tips

Debugging Like the Pros

Debug like a programmer! Here's a reusable script that will display a pop-up for debugging your scripts.

I’ve started assembling a list of useful subroutines, functions, and other code snippets that I use all the time, and I thought I’d share a few with you. A couple columns back you saw my TestPing() function, which I use all the time; next, there's my DebugWindow subroutine. Just call this subroutine with any message you want and it displays that message in a pop-up IE window. It's perfect for adding debug code to your scripts:

Dim oIE
Sub Debug(strText)
  'uncomment the next line to turn off debugging
  'Exit Sub
  If Not IsObject(oIE) Then
    Set oIE = CreateObject("InternetExplorer.Application")
    oIE.Navigate "about:blank"
    oIE.Visible = True
    oIE.ToolBar = False
    oIE.Width = 200
    oIE.Height = 300
    oIE.Left = 10
    oIE.Top = 10
    Do While oIE.Busy
      WScript.Sleep 100
    oIE.Document.Body.InnerHTML = "<b>" & Now & "</b><br>"
  End If
  oIE.Document.Body.InnerHTML = _
  oIE.Document.Body.InnerHTML & strText & "<br>" & VbCrLf
End Sub

When you’re done debugging, uncomment the Exit Sub line and the debug text goes away. I get into the habit of adding this to all my new scripts, and I’ll have it spit out all kinds of status messages so I can keep track of what the script is doing:

Debug "attempting to connect to computer"

Similarly, here’s a subroutine I use to create log files. Just give it a filename and the text to log; if the file exists, it’ll append to it; otherwise, it’ll create the file from scratch:

Sub LogFile(strFilename, strText)
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Dim objTS
  Set objTS = objFSO.OpenTextFile(strFilename,8)
  objTS.WriteLine strText
End Sub

Hopefully you’ll find these useful in your own scripts!

About the Author

Don Jones is a multiple-year recipient of Microsoft’s MVP Award, and is an Author/Evangelist for video training company Pluralsight. Don is also a co-founder and President of, a community dedicated to Microsoft’s Windows PowerShell technology. Don has more than two decades of experience in the IT industry, and specializes in the Microsoft business technology platform. He’s the author of more than 50 technology books, an accomplished IT journalist, and a sought-after speaker and instructor at conferences worldwide. Reach Don on Twitter at @concentratedDon, or on Facebook at

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.