The PowerShell Basics If Statement–#SQLNewBlogger

·

Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

This is a fairly simple construct, but I keep looking up the syntax if I haven’t written anything for a couple of weeks, which does happen. I’m hoping this quick post will help me remember the structure.

Parenthesis and Braces

The general structure is simple. It’s like this:

if ($a -eq 1) {
# do something  
}

This structure has the test expression inside the parenthesis and then any statements to execute, one or more, inside braces. Fairly simple, as long as you remember the –eq, –gt, –lt, etc.

If you have an ELSE, then you add that next with the braces again.

if ($a -eq 1) {
# do something  
}
else {
# do something else
}

That is easy to remember, as long as you use one language. I’ve been working more with Python, which is where I think I get confused.

SQLNewBlogger

This was about the 5th or 6th time I looked up the syntax, so I stopped and wrote this. It took only about 10 minutes to do this, no need to do more than mock up code, but show how this works.

Comments

One response to “The PowerShell Basics If Statement–#SQLNewBlogger”

  1. brianary Avatar
    brianary

    It’s the “else-if” that I have a hard time remembering from language to language.

    elif: Python, bash, and F#
    elsif: Perl and Ruby
    elseif: PHP and PowerShell
    else if : C, C++, C#, JavaScript, and just about everything else (if)
    ELSE IF : T-SQL (case-insensitive)
    ElseIf : VB, VBScript, and VB.NET (case-insensitive)

    Like