Brackets in PowerShell Code Question

Building another script and still not fully understanding how to determine where brackets should go. I am receiving an error after running this piece of code.

PS C:\WINDOWS\system32> function Validate-IsJavaInstalled {
if (Get-WmiObject -Namespace ‘root\cimv2\sms’ -Class SMS_InstalledSoftware | where { ($_.ARPDisplayName -eq ‘Java 7 Update 55’)
} else {
Missing closing ‘)’ after expression in ‘if’ statement.

  • CategoryInfo : ParserError: (:slight_smile: , ParentContainsErrorRecordException
  • FullyQualifiedErrorId : MissingEndParenthesisAfterStatement

PS C:\WINDOWS\system32> $Error
Missing closing ‘)’ after expression in ‘if’ statement.

I am not sure what the expression is in the if statement which might help me find the missing ‘)’

Can anyone help me understand how to identify where to find missing ‘)’ in a PowerShell script?

Thanks for all your help and support.

You are actually missing }s and )s, but there is a much simpler way to do what you are trying to do. First, to fix your existing code it should look like this:

 function Validate-IsJavaInstalled {
    if (Get-WmiObject -Namespace ‘root\cimv2\sms’ -Class SMS_InstalledSoftware | 
            Where-Object { ($_.ARPDisplayName -eq ‘Java 7 Update 55’)})
    } #if
    } #else
} #function

But here is the simpler way:

 function Validate-IsJavaInstalled {
    [bool](Get-WmiObject -Namespace ‘root\cimv2\sms’ -Class SMS_InstalledSoftware | 
            Where-Object {$_.ARPDisplayName -eq ‘Java 7 Update 55’})
} #function


Out of curiosity, why would you check that client-side when that data is in the SCCM database. You would just advertise to clients that meet that criteria rather than sending to a number of clients to check locally/

Hi Rob,

To answer your question, I use client side queries because it is a more real-time view of my environment. Where as SCCM inventory has to occur send the data on a inventory cycle to the server and it has to be processed in the inventory for it to report accurately. I agree it works just as well and for most things is perfect but if I have a urgent update and need to target machine with a specific software and version installed this will allow me to check at the client real-time and thus not miss anything that has not yet reported inventory back to the server. I hope that makes sense.

Hi Mike,

Thanks for the information it did fix it also thank you for the alternative way to accomplish what I am doing.

I do have a question for you it feels like it was easy for you to look at my code and identify what was missing and where it was missing from. In an effort for me to learn how to be that awesome is there something you could recommend that teaches how to identify where brackets need to be placed and possibly why that I can read or get access to? I love the help and great advise but I really am trying to learn and am open to any lessons I can get my hands on to get better at writing scripts, functions, and modules in PowerShell.

Thanks again!


I use VS Code to write scripts which have create syntax highlighting to identify those issues, but even with the PowerShell ISE, there are ways to help identify open/close ({[. In the ISE when you highlight a bracket, parens, etc., it’s pair is also highlighted. This may be hard to see with the default color scheme in ISE since the script pane is white and the highlighting is very light grey, but you can change the color scheme by going to tools then options. If you change the color scheme I would recommend using one of the built in themes and not manually changing individual element colors. The “dark console, dark editor” theme will help you see the highlighted brackets. The other thing you can do in ISE is when you have a parens, bracket, etc highlighted, hold down CTRL and press]. This will bounce the cursor between the matching pairs. Finally, and this is a best practice when coding and scripting in any language, whenever you open a parens, bracket, etc, ALWAYS type the closing pair, then type whatever you need inside. VS Code scripting environment helps enforce this. Hope this helps.


This was just the type of information I was hoping to get here.

I appreciate all the insight and information you have provided thanks a million Sir!

Not sure if the forum software in text mode mangled your code, but, what I re-iterate to every person I work with in the shell. Formatting, formatting, formatting.

pick one of the accepted norms, and stick to it. I’d say 90% of the time folks are having issues with simple code, when they are forced to go back through the script and format, usually you’ll pick up immediately when/where you’re missing brackets.

in addition, use a good editor, VS Code/ise, just don’t use a text editor.

Hi David,

Thanks for the insight, just an FYI I write all of my code in PowerShell ISE.

Using the Dark on Dark editor seems to force the brackets to stand out.

Also learning that I need to look for each opening bracket and know each need a closing bracket helps me know where to start looking for a missing closing bracket.

I appreciate all of the information and insight you guys provide it is a great help on my journey learning PowerShell as I am a beginner. What I am doing to learn is going through Pluralsight’s various PowerShell course some of them have example scripts that we can use and learn from by adding functionality to them.

When I basically re-write line by line these scripts I think I am missing little parts and cannot seem to find them from time to time so I come here for help and advise on how to figure them out.

So far this community has been spot on and a great resource.

I am sure over time I will learn both from the courses and from this forum and be able to write scripts totally on my own to solve any situation that might come up. That is my hope!