Script Tips

The Breaking Point

New scripters, take note: VBscript treats ampersands and underscores as if they were nothing, so to speak.

This time around I want to hit on a basic, but often-confusing thing you'll run across in VBScript.

You may know that VBScript allows you to continue a line of code onto the next line of text, for when a line becomes too long to read. For example, this line:

Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk WHERE VolumeName = 'C'")

is too long to fit on one line of text. It wraps, which may leave you wondering whether or not you're supposed to enter it that way (on two lines) when you're actually typing the script. The line continuation character lets you break this into two lines of text, while leaving VBScript to treat it all as one logical line of code:

Set colItems = objWMIService.ExecQuery("Select * " & _
"from Win32_LogicalDisk WHERE VolumeName = 'C'")

I'll occasionally run into new scripters who get confused by the ampersand there. It isn't officially part of the line continuation; the underscore does that all by itself. For example:

sInput = InputBox("Enter some text", _
"Input needed", _
"Some text")

The code doesn't use the ampersand at all. It doesn't need to, because I'm not breaking the line in the middle of a text literal. When you're dealing with a line of code that's made long by a long text literal, you have to take extra steps. Consider again that first example:

Set colItems = objWMIService.ExecQuery("Select * from "Win32_LogicalDisk WHERE VolumeName = 'C'")

I need to break this in the middle of that WMI query, which is a text literal. I'll start by putting in an ampersand:

Set colItems = objWMIService.ExecQuery("Select * " & "from Win32_LogicalDisk WHERE VolumeName = 'C'")

All I'm doing here is breaking the literal string into two pieces and using the ampersand to concatenate them together. Doing so ensures the integrity of the string literal. Then I can place the line continuation character after the ampersand:

Set colItems = objWMIService.ExecQuery("Select * " & _
"from Win32_LogicalDisk WHERE VolumeName = 'C'")

And everything is hunky-dory. A simple trick, and quite useful, but something that's utterly unintuitive if you're just starting out with scripting.

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.

I agree to this site's Privacy Policy.