MCPMag.com

Sign up for our newsletter.

I agree to this site's Privacy Policy.

Windows Tip Sheet

PowerShell Script Blocks

Script blocks -- one antidote for admin-related repetitive strain injury.

PowerShell is an interesting product, to be sure. It is not only Microsoft’s next generation administrative shell, it's a scripting language. One helpful feature is the ability to save a block of PowerShell script in a variable. When you need to run it, all you need to do is invoke the variable and your "script" will run. Here’s an example:

PS C:\ > $stopped={get-service | where {$_.status -eq "stopped"}}

It's an expression that I might run a few times. So, instead of having to type it every time, I can save it to a variable called $stopped. Everything within the outer pair of curly braces will be executed when I invoke the variable using the ampersand (&) character, like this:

PS C:\ > &$stopped

Status Name DisplayName
------ ---- -----------
Stopped AcrSch2Svc          Acronis Scheduler2 Service
Stopped ALG                 Application Layer Gateway Service
Stopped AppMgmt             Application Management
Stopped Browser             Computer Browser
Stopped CertPropSvc         Certificate Propagation
Stopped clr_optimizatio...  Microsoft .NET Framework NGEN ...
Stopped COMSysApp           COM+ System Application
Stopped dot3svc             Wired AutoConfig
Stopped ehRecvr             Windows Media Center Receiver ...
Stopped ehSched             Windows Media Center Scheduler ...
...

I’ve truncated the output, but you get the idea. The contents of $stopped are known as a script block. There’s no limit to what you can do with this construct. Anything you can type at a PowerShell prompt you can put into a script block -- you don’t have to write a full-fledged PowerShell script. Put your reusable code in a script block and save yourself the effort to typing it more than once.

Tech Help—Just An
E-Mail Away

Got a Windows, Exchange or virtualization question or need troubleshooting help? Or maybe you want a better explanation than provided in the manuals? Describe your dilemma in an e-mail to the MCPmag.com editors at editor@mcpmag.com; the best questions get answered in this column and garner the questioner with a nifty Redmond T-shirt.

When you send your questions, please include your full first and last name, location, certifications (if any) with your message. (If you prefer to remain anonymous, specify this in your message, but submit the requested information for verification purposes.)

If you have many script blocks you use frequently, add them to your Powershell profile. If you forget the name of the variable you used, this:

PS C:\ > dir variable: | sort name

will list all your variables and, hopefully you’ll recognize your script block. Find out more by running:

PS C:\> Help about_script_block

About the Author

Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for MPCMag.com and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at PowerShell.org. Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide. You can keep up with Jeff at his blog http://jdhitsolutions.com/blog, on Twitter at twitter.com/jeffhicks and on Google Plus (http:/gplus.to/JeffHicks)

comments powered by Disqus

Reader Comments:

Sun, Oct 14, 2007 DK Anonymous

While certainly powerful, the ability to use these blocks as a resource would require you to create a powershell blocks script to set all the variables before you intended to use them. You would have to, as a matter of practice, run this blocks script to load the variables into memory before you were able to use any of the script blocks. I assume that each time you close powershell you'd lose the block assignments of variables, so you'd have to reload this script to reset the variables every time you wanted to use them in powershell.

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above