Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell snippets to avoid scripting demands.
Hey, Scripting Guy! One of the things that I find myself having to do from time-to-time is write scripts. I really don’t like to write scripts, but sometimes I cannot find what I need to, and I cannot avoid it. I really wish that when I do have to write a script, I could make it as painless as possible. Is painless possible?
Microsoft Scripting Guy, Ed Wilson, is here. Well tonight is the Charlotte PowerShell Users Group meeting. We will be having a guest presentation by Microsoft PFE, Jason Walker. Jason is a really cool dude, and he has written quite a few guest Hey, Scripting Guy! Blog posts (in fact, he is an Honorary Scripting Guy). So, the meeting tonight will be awesome. The Scripting Wife and I have been looking forward to it for a while.
RA, something else that is awesome is using Windows PowerShell ISE snippets to reduce the amount of scripting involved in creating a custom solution.
When I open the Windows PowerShell ISE, the first thing I need to do is start the snippets. To do this, I can type <CTRL +J>, or simply select Start Snippets from the Edit menu:
The snippet drop-down list appears and it shows me what snippets are available. The list is shown here:
If I hover my mouse over the snippet for a few seconds, the actual code that comprises the snippet appears. This will appear on the left if it is a long piece of code, such as the complete advanced function. It will appear on the right if it is relatively short, such as the comment block. Here is the comment-block snippet:
If I want to insert the code snippet into my script, I highlight the snippet from the drop-down list, and press ENTER. The current insertion point in the script governs where the code snippet will appear. This means that I need to place my mouse cursor exactly where I want the snippet to appear in my script. So the following comment block appears at the beginning of my script because that is where my cursor is:
The cool thing about this snippet is that it moved my insertion point to the place where I would more than likely actually type. Of course, the fact that I have a comment in the middle of a comment block is a bit strange—but hey, it works, and it helps me avoid typing.
Now I decide to add a For loop. I move my insertion point after my comment block, and start the snippets again (I like to use <CTRL + J> because it keeps my hands on the keyboard). I select For from the drop-down list. The description tells me it is a For loop. I press ENTER, and voila! The For loop appears in my code.
The strange thing is that I am left on the For line, not in the code block. But I guess that makes sense because most of the time I am not going to loop 99 times. I add the output for $i into the script block, and I press the green triangle (or <F5>) to run my script. The output is shown here:
I place my insertion point after the $i that I added to the script, start the snippets again, and add an If statement to my script. I have to change the $x variable to $i, and set a condition that $i is equal to 5. I then add code in the script block so that the script will break when the $i condition matches. I then run the script again. The script and the output are shown here:
So I did very little typing, and in less than a minute, I had a script “that does something.” Windows PowerShell code snippets offer two advantages:
- They help if one cannot remember exact syntax. In this way, they often provide enough so that one can quickly get the code written without having to look up everything.
- They help an experienced scripter avoid a lot of typing. In this way, the snippets help save time.
RA, that is all there is to using Windows PowerShell ISE snippets. Script Without Scripting Week will continue tomorrow when I will talk about creating snippets.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy