Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8089727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:29:42+00:00 2026-06-05T19:29:42+00:00

In PowerShell, syntax for if is as so: if (<test1>) {<statement list 1>} [elseif

  • 0

In PowerShell, syntax for if is as so:

if (<test1>)
    {<statement list 1>}
[elseif (<test2>)
    {<statement list 2>}]
[else
    {<statement list 3>}]

Another syntax rule is that for subexpressions, you need to use parentheses like this:

 write-output (get-date)

So with these two rules combined, I would expect that the test for some path needs to be written with two sets of parentheses like this:

if ((Test-Path ...)) {
    # do something
}

However, this also works:

if (Test-Path ...) {
    # do something
}

and just for the sake of completeness, this doesn’t work:

if (!Test-Path ...) {
    # do something
}

(here, you would need to wrap the subexpression in parenthesis as usual).

Can anyone explain the syntax rules that apply here and how comes that I can use the IF test with one parenthesis only? Is it some PowerShell magic or am I misunderstanding the basic syntax rules?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:29:43+00:00Added an answer on June 5, 2026 at 7:29 pm

    Referring to C.2.2 from Appendix C: The PowerShell grammar in Bruce Payette’s Windows PowerShell in Action, we have:

    <ifStatementRule> =
      'if' '(' <pipelineRule> ')' <statementBlockRule>
      [ 'elseif' '(' <pipelineRule> ')' <statementBlockRule> ]*
      [ 'else' <statementBlockRule> ]{0|1}
    

    This indicates the ( and ) tokens as part of the literal syntax for recognizing an if statement, and that the <test> from the about_If documentation refers to a pipeline that will be resolved to a Boolean.

    Following the pipeline rules, we find:

    • Test-Path ... parses to a <cmdletCall> of <name> <parameterArgumentToken>,
    • !Test-Path ... results in an <expressionRule> of <UnaryOperatorToken> <propertyOrArrayReferenceRule>, which fails when the cmdlet call cannot match the simple property or array rule, whereas
    • !(Test-Path ...) is able to match the parenthesized cmdlet call as a sub-expression.

    Edit: See also PowerShell 2.0 Language Specification (thanks to Roman’s answer to another question).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So i normally use this syntax to create as symbolic link from powershell. PS
Using powershell, you can use the '&' character to run another application and pass
As new to Powershell world, sometime I'm stuck in the tricky syntax. That's why
I'm completely new to PowerShell and am confused about some of its command syntax.
I am trying to use PowerShell to query The advanced settings of Applicaiton pools
//powershell code $quiotent = divRemainderQuiotent ($a) ($b) ([ref] $remainder) # I need to pass
PowerShell noob question here. I have groups of servers that are listed in separate
Has anyone found a plugin for Visual Studio to allow for PowerShell syntax highlighting
I added Execute PowerShell Script action to my FinalBuilder project. Manual says that I
I have a powershell script that modifies transport rules. It works perfectly from powershell

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.