Professional Documents
Culture Documents
about_Script_Blocks
SHORT DESCRIPTION
Defines what a script block is and explains how to use script blocks in
the Windows PowerShell programming language.
LONG DESCRIPTION
In the Windows PowerShell programming language, a script block is a
collection of statements or expressions that can be used as a single unit.
A script block can accept arguments and return values.
Syntactically, a script block is a statement list in braces, as shown in
the following syntax:
{<statement list>}
A script block returns the output of all the commands in the script block,
either as a single object or as an array.
Like functions, a script block can include parameters. Use the Param
keyword to assign named parameters, as shown in the following syntax:
{
param ([type]$parameter1 [,[type]$parameter2])
<statement list>
}
In a script block, unlike a function, you cannot specify parameters outside
the braces.
Like functions, script blocks can include the DynamicParam, Begin, Process,
and End keywords. For more information, see about_Functions and
about_Functions_Advanced.
Using Script Blocks
A script block is an instance of a Microsoft .NET Framework type
(System.Management.Automation.ScriptBlock). Commands can have script
block parameter values. For example, the Invoke-Command cmdlet has a
ScriptBlock parameter that takes a script block value, as shown in this
example:
C:\PS> invoke-command -scriptblock { get-process }
Handles NPM(K)
PM(K)
WS(K) VM(M) CPU(s)
------- -------------- ----- -----999
721
. . .
28
28
39100
32696
45020
36536
262
222
15.88
20.84
Id ProcessName
-- ----------1844 communicator
4028 explorer