Skip to main content

What do Parenthesis do in PowerShell?

Depending on where you are using them, parenthesis can be used for several different functions.

In Math

If you remember back in algebra class, we used parenthesis to change the order of processing a math equation.  Normally multiplication and division is performed before addition and subtraction

PS C:\> 5+6*10

65

Add some parenthesis, and you change the order of processing.

PS C:\> (5+6)*10

110

 

In Cmdlets

We can use parenthesis in cmdlets to force other cmdlets to run first.  In this example, we have a text file called Names.txt.  It contains the names of three computers.  Using the cmdlet Get-Content we can see these names.

PS C:\psworks> Get-Content names.txt

TechTour-CL1

TechTour-DC1

Tech-ex1

Now we are going to ask for the BIOS information from each of these clients.  there are two ways to do this.  The long way is where we specify each computer name one at a time.

PS C:\psworks> Get-WmiObject Win32_Bios -ComputerName Tech-EX1

 

 

SMBIOSBIOSVersion : 090006

Manufacturer      : American Megatrends Inc.

Name              : BIOS Date: 05/23/12 17:15:53  Ver: 09.00.06

SerialNumber      : 7563-3642-6655-4653-5455-8957-02

Version           : VRTUAL - 5001223

 

Here we would have to type this command 3 times, changing the value of ComputerName name each time.  The still long, but not as long method is to provide all the names in a comma separated list.

PS C:\psworks> Get-WmiObject Win32_Bios -ComputerName Tech-EX1, TechTour-DC1, TechTour-CL1

 

 

SMBIOSBIOSVersion : 090006

Manufacturer      : American Megatrends Inc.

Name              : BIOS Date: 05/23/12 17:15:53  Ver: 09.00.06

SerialNumber      : 7563-3642-6655-4653-5455-8957-02

Version           : VRTUAL - 5001223

 

SMBIOSBIOSVersion : 090006

Manufacturer      : American Megatrends Inc.

Name              : BIOS Date: 05/23/12 17:15:53  Ver: 09.00.06

SerialNumber      : 2500-5916-2465-4853-2519-9911-18

Version           : VRTUAL - 5001223

 

SMBIOSBIOSVersion : 090006

Manufacturer      : American Megatrends Inc.

Name              : BIOS Date: 05/23/12 17:15:53  Ver: 09.00.06

SerialNumber      : 0214-6255-9078-8822-9031-3935-43

Version           : VRTUAL - 5001223

 

This works, but is very static.  We would have to do a lot of typing if we wanted to add more clients to the list.  Another way would be to use a txt file.  We will encapsulate The Get-Content cmdlet inside of parenthesis so it executes first.  It will feed one name at a time to the ComputerName parameter.

PS C:\psworks> Get-WmiObject Win32_Bios -ComputerName (Get-Content Names.txt)

 

 

SMBIOSBIOSVersion : 090006

Manufacturer      : American Megatrends Inc.

Name              : BIOS Date: 05/23/12 17:15:53  Ver: 09.00.06

SerialNumber      : 0214-6255-9078-8822-9031-3935-43

Version           : VRTUAL - 5001223

 

SMBIOSBIOSVersion : 090006

Manufacturer      : American Megatrends Inc.

Name              : BIOS Date: 05/23/12 17:15:53  Ver: 09.00.06

SerialNumber      : 2500-5916-2465-4853-2519-9911-18

Version           : VRTUAL - 5001223

 

SMBIOSBIOSVersion : 090006

Manufacturer      : American Megatrends Inc.

Name              : BIOS Date: 05/23/12 17:15:53  Ver: 09.00.06

SerialNumber      : 7563-3642-6655-4653-5455-8957-02

Version           : VRTUAL - 5001223

 

Now we only need to update the txt file when we add or remove a client.  Also, we can reuse this list.  If you have access to the Active Directory Module, you could just pull the list for AD itself and not have to worry about updating a text file.

PS C:\psworks> Get-WmiObject Win32_Bios -ComputerName ((Get-ADComputer -Filter *).Name)

 

 

SMBIOSBIOSVersion : 090006

Manufacturer      : American Megatrends Inc.

Name              : BIOS Date: 05/23/12 17:15:53  Ver: 09.00.06

SerialNumber      : 2500-5916-2465-4853-2519-9911-18

Version           : VRTUAL - 5001223

 

SMBIOSBIOSVersion : 090006

Manufacturer      : American Megatrends Inc.

Name              : BIOS Date: 05/23/12 17:15:53  Ver: 09.00.06

SerialNumber      : 0214-6255-9078-8822-9031-3935-43

Version           : VRTUAL - 5001223

 

SMBIOSBIOSVersion : 090006

Manufacturer      : American Megatrends Inc.

Name              : BIOS Date: 05/23/12 17:15:53  Ver: 09.00.06

SerialNumber      : 7563-3642-6655-4653-5455-8957-02

Version           : VRTUAL - 5001223

 

Notice that we have 2 pairs on parenthesis.  One inside of the other.  The inner pair executes the Get-ADComputer –Filter * command.  Inside the outer pair, we are simpling dotting out the Name parameter of the resulting three computer objects and feeding them one at a time to the ComputerName parameter.

In Subexpressions

When you display string of information on the screen for an object, odd things can happen.  Take a look at this example.  This is how a string should look when it is displayed.

PS C:\psworks> "This is a string"

This is a string

 

In PowerShell, we often work with objects.  If you want to display the contents of an object on screen, things can get a bit messy.  This first line places the object generated by the cmdlet Get-Host into the variable $PS.

PS C:\psworks> $PS = Get-Host

 

When we dump the contents of memory to the screen, we get this:

PS C:\psworks> $PS

 

 

Name             : Windows PowerShell ISE Host

Version          : 4.0

InstanceId       : 7259e6c3-6dc0-4abe-932a-9fbfe9d3a683

UI               : System.Management.Automation.Internal.Host.InternalHostUserI

                   nterface

CurrentCulture   : en-US

CurrentUICulture : en-US

PrivateData      : Microsoft.PowerShell.Host.ISE.ISEOptions

IsRunspacePushed : False

Runspace         : System.Management.Automation.Runspaces.LocalRunspace

 

Let’s say we are interested in just the Version property number.

PS C:\psworks> $PS.Version

 

Major  Minor  Build  Revision

-----  -----  -----  --------

4      0      -1     -1    

 

We can see that this is also an object.  We want the Major property.

PS C:\psworks> $PS.Version.Major

4

 

Our goal is to have a string that reads “My version of PowerShell is 4”.  Let’s try this without using a subexpression.

PS C:\psworks> "My version of PowerShell is $PS.Version.Major"

My version of PowerShell is System.Management.Automation.Internal.Host.Internal

Host.Version.Major

 

Not exactly what we want.  Whenever you want to display the property information of an object, you need to use a subexpression.  A subexpression tells PowerShell to execute what is inside of my parenthesis as if it were code.  Our subexpression would look like this $($PS.Version.Major). Let’s take a look at it in our example.

PS C:\psworks> "My version of PowerShell is $($PS.Version.Major)"

My version of PowerShell is 4

 

Much better.

 

In a construct

Parenthesis are also used in our scripting constructs. Take a look at this simple IF statement.

$X = 3

If ($X -gt 1) {Write-Host "Greater than 1"}

 

In this example, the code inside of the parenthesis are being evaluated for True or False.  When you see parenthesis in a programming construct, make sure to read the help file for that construct so you know how to properly use the parenthesis.  in this case, the help file is About_If.

You will find parenthesis in more complex code of PowerShell, but these cover the most common areas that you will use parenthesis.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

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.