Should I use semicolons at the end of each PowerShell statement?
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.
Related videos on Youtube
Mark Allison
Updated on September 18, 2022Comments
-
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 over 11 yearsAre you talking about semicolons to end the line? this question is offtopic.
-
APR over 11 years@Hex: Why would it be off topic? PowerShell is used to administer you machine, so why not...
-
Mark Allison over 11 years@Hex Yes at the end of a line. Why is it off-topic?
-
Hex over 11 yearsMark 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 over 5 yearsIt'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 over 2 yearsNote 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 almost 2 yearsThe semicolon is unnecessary so it's better to leave it out.