Should I use semicolons at the end of each PowerShell statement?

41,185

Solution 1

Powershell primarily uses new lines as statement seperators, but semicolons may be used for multiple statements on a single line.

Solution 2

The key, no matter whether you choose to use semicolons or not, is to be consistent. If you are used to using them in C#, then continue to use them in PowerShell. If you don't want to use them, then don't use them. Pick a standard and stick with it for you and the people that will read and use your code. It will get ugly if you decide halfway through a project to start using them.

Share:
41,185

Related videos on Youtube

Mark Allison
Author by

Mark Allison

Updated on September 18, 2022

Comments

  • Mark Allison
    Mark Allison over 1 year

    I've done a fair bit of programming in C#, but then I've also written a lot of T-SQL scripts. C# requires semicolons, and T-SQL and PowerShell they're optional. What do you do for PowerShell? Why? My gut feel is to include semicolons but I don't know why.

    • Hex
      Hex over 11 years
      Are you talking about semicolons to end the line? this question is offtopic.
    • APR
      APR over 11 years
      @Hex: Why would it be off topic? PowerShell is used to administer you machine, so why not...
    • Mark Allison
      Mark Allison over 11 years
      @Hex Yes at the end of a line. Why is it off-topic?
    • Hex
      Hex over 11 years
      Mark Allison, sorry it was my mistake because I misunderstood your question. In linux powershell you do not need to add semicolons same as you dont need to add semicolons in MS DOS.
    • Brain2000
      Brain2000 over 5 years
      It's because there are squiggly brackets. Those and semicolons are like chocolate and peanut butter. They're inseparable. I blame Kernighan and Ritchie..
  • Anthony DiSanti
    Anthony DiSanti over 2 years
    Note that there is a practical difference between running each statement as its own command versus running multiple statements within a single command, as can be seen in this question. My personal decision is to not use semi-colons so that my test results in an interactive session align with what my script will receive when it runs the commands.
  • Admin
    Admin almost 2 years
    The semicolon is unnecessary so it's better to leave it out.