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.
Asked
Active
Viewed 4.8k times
59
2 Answers
79
Powershell primarily uses new lines as statement seperators, but semicolons may be used for multiple statements on a single line.
Zoredache
- 131,987
-
1Note 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. – Anthony DiSanti Sep 03 '21 at 18:24
-
1
26
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.
Andy Schneider
- 1,553
;) as Line Terminators PowerShell will not complain about extra semicolons, but they are unnecessary, and can get in the way when code is being edited or copy-pasted. They also result in extra do-nothing edits in source control when someone finally decides to delete them. They are also unnecessary when declaring hashtables if you are already putting each element on its own line. – iRon Sep 07 '22 at 09:58