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

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_NTDomainLook for the DomainControllerName property.

Test to see what that data type of a value is in PowerShell

PowerShell has a comparison operator called –is.  The –is operator simply response True or False when you use it to verify the data type of a value.  The valid data types in PowerShell are:
[string]    Fixed-length string of Unicode characters
[char]      A Unicode 16-bit character
[byte]      An 8-bit unsigned character
[int]       32-bit signed integer
[long]      64-bit signed integer
[bool]      Boolean True/False value
[decimal]   A 128-bit decimal value
[single]    Single-precision 32-bit floating point number
[double]    Double-precision 64-bit floating point number
[DateTime]  Date and Time
[xml]       Xml object
[array]     An array of values
[hashtable] Hashtable object

Below is a script that will use –is to test some values.
$String="Hello"$Boolean=$True$Int=15Write-Host"Test for string"$String-is [String] $Boolean-is [String] $Int-is [String] Write-Host" "Write-Host"Test for Boolean"…

Where did a User’s Account Get Locked Out?

Updated: May 15, 2015
When this article was originally published, two extra carriage returns were add causing the code to malfunction.  The code below is correct.  

My client for this week’s PowerShell class had a really interesting question. They needed to know where an account is being locked out at. OK, interesting. Apparently users hop around clients and forget to log off, leading to eventual lock out of their accounts. The accounts can be unlocked, but are then relocked after Active Directory replication.
This problem is solved in two parts. The first one is to modify the event auditing on the network. The second part is resolved with PowerShell.
The first part involves creating a group policy that will encompass your Domain Controllers. In this GPO, make these changes.
Expand Computer Configuration \ Policies \ Windows Settings \ Security Settings \ Advanced Audit Policy Configuration \ Audit Policies \ Account ManagementDouble click User Account ManagementCheck Configure the f…