• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Stephanos Constantinou Blog

PowerShell Scripting

  • Home
  • Blogs
    • PowerShell Tutorials
    • PowerShell Scripts
    • PowerShell Modules
      • Modules Cmdlets
    • Software Reviews
  • About
  • Contact
You are here: Home / PowerShell Tutorials / PowerShell Script Blocks

PowerShell Script Blocks

31/08/2018 by Stephanos Leave a Comment

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:

PowerShell Script Blocks - Example 1

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 - Example 2

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:

PowerShell Script Blocks - Example 3

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:

PowerShell Script Blocks - Example 4

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:

PowerShell Script Blocks - Example 5

[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:

PowerShell Script Blocks - Example 6

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”]

Summary
PowerShell Script Blocks
Article Name
PowerShell Script Blocks
Description
PowerShell Script Blocks. In this tutorial you will find information about PowerShell Script Blocks and their use. Stephanos Constantinou Blog
Author
Stephanos
Publisher Name
Stephanos Constantinou Blog
Publisher Logo
Stephanos Constantinou Blog

Filed Under: PowerShell Tutorials Tagged With: Arithmetic Operators, Get-ChildItem, Get-NetAdapter, Invoke-Command, Rename-Item, Write-Host

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Footer

Recent Posts

  • ICS Cube Product Review 26/04/2019
  • PowerShell Module SysInfo v1.2.0 15/03/2019
  • PowerShell Module SysInfo v1.1.2 13/11/2018
  • PowerShell Module SysInfo 24/10/2018
  • Get-VoltageProbe 24/10/2018
  • Get-VideoController 24/10/2018
  • Get-USBController 24/10/2018
  • Get-TrackPoint 24/10/2018
  • Get-TrackBall 24/10/2018
  • Get-TouchScreen 24/10/2018
Planet PowerShell

Categories

  • Modules Cmdlets (57)
  • PowerShell Modules (5)
  • PowerShell Scripts (38)
  • PowerShell Tutorials (35)
  • Software Reviews (2)

Archives

  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (1)
  • October 2018 (56)
  • September 2018 (13)
  • August 2018 (9)
  • July 2018 (6)
  • June 2018 (8)
  • May 2018 (7)
  • April 2018 (9)
  • March 2018 (4)
  • February 2018 (6)
  • January 2018 (12)
  • December 2017 (4)
Top 10 PowerShell 2018

Blogroll

  • Planet PowerShell
  • Reddit – PowerShell
  • PowerShell Magazine
  • PowerShell.org
  • PowerShell Team Blog
  • Hey, Scripting Guy! Blog
  • Mike F Robbins
  • PowerShell Explained with Kevin Marquette
  • Mike Kanakos – Network Admin
  • The Lonely Administrator
  • AskME4Tech
PowerShell Blogs Sysadmin Blogs Banners for Top 20 Programming Blogs

© 2023 · Stephanos Constantinou Blog

  • Home
  • Blogs
  • About
  • Contact