Skip to main content

When to use [] or {} or ()

Yesterday I posted a question that comes up often in my PowerShell classes on when to use special variables. Today I am going to post another one. When to open and close code and what characters to use.

Indexes of Arrays [ ]

The Square braces are used when we want to access an element of an array, also known as a collection.

# Accessing Array Elements

 

# Create an array.

$Fruit = "Apple", "Grape", "Orange", "Pear"

 

# Access the first element of the array using index 0.

$Fruit[0]

 

# Access the second element of the array using index 1.

$Fruit[1]

 

# Access the last element in an array using -1.

$Fruit[-1]

 

# Access the last element in an array using -2.

$Fruit[-2]

OK, that was easy. Not so for the next two.

 

Using Script Blocks { }

The curly braces are used with script blocks. A script block allows you to take multiple lines of code and use them as a single unit. Many commands can utilize script blocks. Script Blocks are used when declaring functions, classes, workflows, enums, hash tables, and DSC. They are also used in cmdlets with parameters such as ScriptBlock, Begin, Process, and End. Also IF and SWITCH statements use them.  The help files of cmdlets will guide you in how to use them. Here are a few examples.

# ScriptBlock used in a functions

Function MyFunction

{

    Write-Host "Hello"

}

 

# ScriptBlock used in a remote command

Invoke-Command -ComputerName DC2 -ScriptBlock {Get-Process}

 

# ScriptBlock used in the pipeline with ForeEach-Object

Get-Process |

    ForEach-Object -Process {$_ | Select-Object -ExpandProperty Name}

 

Using Parenthesis ( )

Parenthesis can be used in a few different ways. We can use them to control the order of operation of math.

# Parenthesis used to control the order of a math operation

 

4 + 5 * 2

(4 + 5) * 2

We also use them to execute a cmdlet first in order to gain direct access to the object that the cmdlet produces.

# Parenthesis used get immediate access to the object a cmdlet produces

 

Get-Date | Select-Object -ExpandProperty DayOfWeek

(Get-Date).DayOfWeek

When we are going to do a comparison operation, such as for an IF or SWITCH statement, we use parenthesis to denote the comparison operation.

# Parenthesis used in an IF statement

 

If (1 -lt 5) {Write-Host "It Works"}

 

# Parenthesis used in an SWITCH statment

Switch ("PowerShell")

{

    "PowerShell" {Write-Host "PowerShell"}

    "ShellPower" {Write-Host "ShellPower"}

 

}

We also use the parenthesis with the PARAM keyword.

# Parenthesis used with the PARAM keyword

Function MyCode

{

Param ($Num1, $Num2)

    Write-Output ($Num1 + $Num2)

 

}

 

MyCode -Num1 10 -Num2 5

We also use parenthesis with sub expressions.

# Parenthesis in sub expressions

$Procs = Get-Process | Select-Object -First 1

Write-Output "The process name is $Procs.Name"

 

Write-Output "The process name is $($Procs.Name)"

If you are a bit frustrated when all the different ways to use parenthesis, I do not blame you. They are also used in other loop constructs to. It will come down to your knowledge of PowerShell. That will come with time. Remember the help files are there to guide you. Use them. The examples will show you which set of characters to use with each cmdlet or parameter if required.

Comments

Popular posts from this blog

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the ma...