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

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...