Weekend Scripter: Space: Not the Final Frontier in PowerShell

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, talks about spacing in Windows PowerShell scripts.

Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sipping a cup of English Breakfast tea. I added some orange peel and fresh squeezed Meyer lemon juice to it. I also added a cinnamon stick. The result is nice and tangy—great to help one to wake up in the morning. I was at an international market the other day, and I picked up a bag of instant scone mix. I thought I would give it a try. They are not bad is the final mark…not too bad at all. Certainly not on par with a freshly made scone from scratch. But still, this mix only requires one to add a bit of water, mix a bit, plop pieces on a baking sheet, then pop them in the oven for a few minutes. They are ready nearly at the same time as my pot of tea.

I've been asked several times if the use of spaces in a Windows PowerShell script makes any difference. The answer (it may surprise you) is that it depends. For one thing, it certainly makes a difference for readability. Here are a few examples…

One thing I notice students doing at times, is worrying about spaces between variables, equality operators, and cmdlets. In reality, it does not matter. If a script is not working, it is not because of a space between these things. The following image shows that both commands will work, but with proper spacing, the commands are easier to read.

Image of command output

Spaces in expressions also make things easier to read, but they do not affect the code. It is common to put a space after the "for" in a for statement, and between the ranges and the script block. But as is shown in the following image, it is not necessary to make the code work. So once again, if a script is not working, wasting a lot of time placing spaces between every member is not going to make the script work any better. It might facilitate troubleshooting, however, in that it may make the code easier to read and understand.

Image of command output

On the other hand, a Here string is very picky about spacing and the like. For example, I cannot have a string on the top line of the Here string. It will generate an error message. This is shown here:

Image of command output

The header of the Here string must be on a line alone. I can have spaces after the @”, but not any text. This i shown here:

Image of command output

By the same token, I cannot have the closing “@ on the same line as other text. It will also generate an error message. This is shown here:

Image of command output

So I put the “@ on a line alone. In fact, it is where the insertion point put me when I pressed ENTER. Dude, I still get an error message. As shown here, it says I cannot have any white space before the terminator:

Image of command output

Well, the good thing is that the error messages are really good and they help me know what to do. So I erase the white space, put my “@ at position 1, and all is groovy. This is shown here:

Image of command output

That is it for white space. Join me tomorrow when I will talk about adding line breaks and not breaking Windows PowerShell scripts.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon