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

How to force a DNS zone to replicate

For many implementations of DNS in a Windows environment, DNS is configured as being Active Directory integrated.  In other words, the DNS zone information is actually stored as a partition in the active directory database.  When Active Directory replicates, the zone data transfers.  For standard DNS deployments, the data is stored in a file.  You have to configure zone transfers manually in the DNS console.   The question in class was how to initiate replication manually.  Once you have properly configured a Primary and secondary DNS server and configured the Primary server to allow zone transfers, you can manually initiate a zone transfer.   Below you can see our test environment.  The image is of to RDP sessions to two different servers.  The DNS console on the left is the primary.  You can see and entry for Test2 that is not in the secondary database.  The servers are named NYC-DC2 (Primary DNS) and NYC-DC1 (Secondary DNS).  The DNS zone is named test.contoso.com . On the se

Disable SMB signing

It never fails.  Once ever couple of months I have a delegate in my class that has to keep a Windows NT4 box running.  There is nothing wrong with that.  Many applications build on Windows NT4 are solid.  Why upgrade and incur cost when no upgrade is really required?  That is generally the reason why Windows NT4 is being used.  Another reason is the vender went out of business, but the application that is required is really good and paid for. Two things to take note of.  If these Windows NT4 clients are going to be authenticating on a Windows Sever 2008 DC, then you may have a problem.  For WinNT 4.0 SP2 and earlier, SMB signing was not supported.  For WinNT4.0 SP3 and earlier, secure channel was not supported. SMB signing helps to prevent Man-in-the-middle attacks.  To open GPMC, click Start , click Run , type gpmc.msc , and then click OK . In the console tree, right-click Default Domain Controllers Policy in Domains\ Current Domain Name \Group Policy objects\Default Domain Co