PowerShell Script Blocks
In this tutorial we will see about PowerShell Script Blocks. A PowerShell scripting block is a list of statements and expressions that you are able to use them as a single unit. When you setup a script block, you can configure it to accept arguments. A script block can also return values back.
The syntax to define a script block is to close the list of statements and expressions in curly brackets (braces). Check below:
{<list of statements and expressions>}
The output of the commands that are in the script block, will be returned in the script block. The output might be an array of values or as a single object.
PowerShell script blocks can have parameters, similar to functions. And also they are able to accept the below keywords:
DynamicParam
Begin
Process
End
Moreover, unlike functions, you are not able to specify parameters outside of the braces. Check the syntax below:
{ param( [type]$Variable1, [type]$Variable2, [type]$Variable3) <list of statements and expressions> }
[adinserter name=”In Article”]
How to use script blocks
A script block is an instance of a Microsoft .NET Framework type (System.Management.Automation.ScriptBlock). Some commands have -ScriptBlock
parameter that you are able to assign values. An example of such command is Invoke-Command
. Let’s see few examples on how we are able to use PowerShell script blocks.
Example 1
In this example we will use the Invoke-Command
with a simple script block.
Code:
Invoke-Command -ScriptBlock {Get-NetAdapter -Physical}
Output:
For our next example we will include parameter in out script block. By using Invoke-Command we are able to provide arguments to the script block to be assigned to our parameters. Let’s see our example.
Example 2
Code:
Invoke-Command -ScriptBlock { param( $input = "Sunny") Write-Host "$input Weather"} Invoke-Command -ScriptBlock { param( $input = "Sunny") Write-Host "$input Weather"} -ArgumentList "Bad"
Output:
PowerShell script blocks can also be assigned to variables. By assigning the script block to a variable allows you to call the script whenever you need it by using that variable. Based on the above examples we can keep our script blocks in variables like below:
Code:
$MySB1 = {Get-NetAdapter -Physical} $MySB2 = { param( $input = "Sunny") Write-Host "$input Weather"}
If you will call the script block as part of a command, you will only need to fill the variable after the parameter. You are able also to call a script block directly, by using the call operator &
. Using &
will allow you to call the script block and run the statements inside the script block. So based on the above examples lets see another way to call our script blocks.
[adinserter name=”In Article”]
Example 3
Code:
$MySB1 = {Get-NetAdapter -Physical} Invoke-Command -ScriptBlock $MySB1 & $MySB1
Output:
Example 4
Code:
$MySB2 = { param($a = "Sunny") Write-Host "$a Weather"} Invoke-Command -ScriptBlock $MySB2 Invoke-Command -ScriptBlock $MySB2 -ArgumentList "Bad" & $MySB2 & $MySB2 "Bad"
Output:
Another way to call a script block is to save it in a variable along with the call operator. In this case if you call use the variable it will run the script block immediately.
Example 5
Code:
$MySB2 = & { param($a = "Sunny") Write-Host "$a Weather"} $MySB2 = { param($a = "Sunny") Write-Host "$a Weather"} $SB = & $MySB2 $SB = & $MySB2 "Bad" $b = "Bad" $MySB2 = { param($a = "Sunny") Write-Host "$a Weather"} $SB = & $MySB2 $b
Output:
[adinserter name=”In Article”]
Delay-Bind Script Blocks
A delay-bind script block allows you to pipe input to a given parameter, then use script blocks for other parameters using the pipeline variable $_
to reference the same object.
Example 6
Code:
Get-ChildItem -Path "C:\TestFolder" Get-ChildItem -Path "C:\TestFolder" | Rename-Item -NewName {"test-" + $_.Name} Get-ChildItem -Path "C:\TestFolder" $SB = {"new-" + $_.Name} Get-ChildItem -Path "C:\TestFolder" | Rename-Item -NewName $SB Get-ChildItem -Path "C:\TestFolder"
Output:
I hope the tutorial about PowerShell Script Blocks is helpful.
Please let me know your comments and thoughts.
You feedback is appreciated.
[adinserter name=”In Article”]
Related Links:
- PowerShell Scripts
- PowerShell Tutorials
- about_Script_Blocks | Microsoft Docs
- Invoke-Command – Microsoft Docs
- PowerShell Arithmetic Operators
- Get-ChildItem – Microsoft Docs
- Rename-Item – Microsoft Docs
- Get-NetAdapter – Microsoft Docs
- Write-Host – Microsoft Docs
[adinserter name=”Matched-Content”]


Leave a Reply