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 [email protected]; 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 an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular