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

Export Your Performance Monitor Data to Excel

Updated: 2016MAY04 To clarify when this functionality is available, you can only save the view when you are viewing a Data Collection Set.  The "live" data cannot be saved in this way. Performance Monitor in Windows Server give us the ability to see when our servers are having some issues.  Analyzing that data into something meaningful can be a problem.  You can export your data to Excel so you can better see what your performance data represents.  First collect your data. Right click the graph and select Save Data As . Change the Save as type to Text file (comma delimited)(*.csv) . Give the file a name and save it where you want to store it. Now open that file on a client with Excel installed on it.  By using excel, you will be able to present the data in a more meaningful format.

Determine which Domain Controller a client is connected to with PowerShell

When a Windows client comes online, it must find a domain controller to bind to.  Either through a static configuration or DHCP, the client will request a list of all Domain Controllers in the domain from a DNS server.  Once the list is received, the client will randomly go through the list to find a DC that will respond.  Once the client has authenticated itself with the DC, the DC will transmit the site information to the client.  The site information will contain the site name, the subnet(s) associated with that site, and any domain controllers in that site.  The client will then take a look at it’s own IP address to determine which site it is in.  From the list of DCs in the same site, it will attempt to bind to one of those DCs to receive it’s Group Policies.   You can use PowerShell and WMI to locate the domain controller that a client is connected to.   Get-WMIObject Win32_NTDomain   Look for the DomainControllerName property.