Prof. Powershell

Super ScriptBlocks

Brace yourself! That's your way of telling PowerShell to execute whatever code is encased in the curly braces.

If you've been using PowerShell for awhile you've seen blocks of code enclosed in a set of curly braces {}. For example:

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

This construct is referred to as a script block. When PowerShell comes across a script block it will execute all the code within the braces. This is how an If statement works:

If ($x -gt 3) {
Write-host "The value is greater than 3"
}

The only time the Write-Host command will execute is when the value of $x is greater than 3. But there is another super way to use script blocks.

You can create a script block and assign it to a variable:

PS C:\> $sb={ps | sort ws -desc | select -first 10}

The script block expression to get the top 10 processes by workingset size has been saved as variable $sb. I haven't run the command, merely stored it in $sb. To run the script block I'll use the & operator:

PS C:\> &$sb

Handles NPM(K)  PM(K)  WS(K) VM(M)  CPU(s)   Id ProcessName
------- ------  -----  ----- -----  ------   -- -----------
    812     14  86224  17240   217   25.75 1912 powershell
   1374     53  16272  13900   101  965.95  880 svchost
    527     14  15216  11064   102   79.70 1320 explorer
    631     35   8604   7000    64   15.66  524 winlogon
    183      7   3916   4488    46    0.77 1048 spoolsv
    173      4   2228   4168    36    0.28 1260 wuauclt
    486     10   4100   3912    41   18.16  580 lsass
    284     10  12628   3700    82   10.86 2528 mmc
    284      7   3968   2996    30    7.66  568 services
    173      5   2104   2760    36    1.19  960 svchost

This is a handy technique for creating a pseudo-alias. The New-Alias cmdlet will only let you create a new alias for a cmdlet, script, function or command. It will fail if you try to create an alias like this:

PS C:\> new-alias -name foo -value "get-service spooler | select *"

Well, technically New-Alias will create the alias but PowerShell will complain when you try to use the alias. The alternative is to use a script block:

PS C:\> $sp={get-service spooler | select *}
PS C:\> &$sp

Name : Spooler
CanPauseAndContinue : False
CanShutdown : True
CanStop : True
DisplayName : Print Spooler
DependentServices : {}
MachineName : .
ServiceName : Spooler
ServicesDependedOn : {RPCSS}
ServiceHandle : SafeServiceHandle
Status : Running
ServiceType : Win32OwnProcess, InteractiveProcess
Site :
Container :

Use script blocks to simplify complex expressions and to provide shortcuts to often used expressions. For more information look at the About_Script_Blocks help file.

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

Upcoming Training Events