r/PowerShell • u/CryktonVyr • Jan 22 '26
Understanding Optimisation with ';' '|' '||' '&' '&&'
Hello Everyone!
I've been learning to code with powershell on and off for 2 years. I recently learned why using the pipeline '|' helps optimising a script.
I already knew how to use '&' and '|' but I just learned today of the possibilities with ';' '||' '&&' and thought I would share and ask a followup questions to our TEACHER OVERLORDS!!!
- semi-colon ';' to Chain commands
(Ex: Clear-Host; Get-Date; Write-Host "Done")
2. Double Pipe Line '||' to execute a 2nd command if the first failed
(Ex: Test-Connection google.ca -Count 1 || Write-Host "No internet?")
3. Double Ampersand '&&' to execute a 2nd command if the first succeeds
(Ex: Get-Date && write-host "TODAY'S THE DAY!!")
Now the question I have is. Is this a good way to optimise a code, how and why?
32
u/skiddily_biddily Jan 22 '26
I didn’t know about double pipe and double ampersand. Thanks for sharing.
5
u/BlackV Jan 22 '26 edited Jan 24 '26
it does not really chain commands, it just being us in-place of a carriage return and is deffo not code optimization, this should only be used in specific (very few) circumstances
See pipeline chain operators HERE and HERE it syntax sugar (i guess you'd call it) and version specific, and personally harder to understand than other constructs, maybe useful in 1 liners but I'll pass
EDIT: Oops brackets wrong way round
8
u/arslearsle Jan 22 '26
Use try catch or check for null in a if then else.
5
u/Kirsh1793 Jan 22 '26 edited Jan 22 '26
TL;DR Don't use those command flow operators in scripts. Feel free to use them interactively in the shell.
PowerShell is a scripting language which can be used interactively in the console, but also to write scripts. Many people coming from other scripting languages dislike PowerShell's verbosity. However, I find it to be very handy in scripts especially. Because, if you don't use aliases, a PowerShell script is quite readable even for a beginner. Because of the Verb-Noun structure of commands, you can infer what a command does. Using aliases, you can drastically minimize typing and shorten commandlines, when using PowerShell in the console.
I don't think the operators you listed should be considered for "optimization". In scripting, I see two lanes for optimization. Speed and maintainability/scalability. In PowerShell they are not the same.
Optimizing for speed, apart from refactoring script logic, usually involves using .NET methods directly instead of the PowerShell native Cmdlet. These .NET methods can lessen readability or at least intimidate beginners, if they are not familiar with them. They can, they don't always do.
Optimizing for maintainability means making the script as readable as possible. It means keeping it as simple as possible (more complexity means more possibility for errors). But it also means keeping it extensible, so you can easily add or remove things, when circumstances change.
';' is useful to chain commands in the console. I cannot think of a use case where I would have to use this in a script. I'd rather scroll up and down when reading a script, not right and left. So, I will gladly put each command on its own line. I will even use splatting for a cmdlet with a lot of parameters to avoid the line becoming too long.
'|' will definitely be useful in the console AND also in scripts. Although consider if you want to pipe commands in a script or if another construct is better suited regarding speed and/or readability. E.g. piping a collection to ForEach-Object is often slower than running a foreach loop over the collection. But chaining foreach loops might be less efficient than doing everything in one pipeline. Because the second foreach loop only starts when the first one has finished. However, when you pipe objects from cmdlet to cmdlet, later cmdlets can already process objects before the first cmdlet is done with the entire collection.
Since I mostly use Windows PowerShell v5.1, I've never actually used '||', '&' or '&&'. Again, I reckon, they are more useful in the console, but I would refrain from using them in a script for two reasons. One, it is not obvious what these operators do - so, less readable. Two, coming from C# or Java, you might confuse them with operators for conditions in if statements.
Sorry for the long comment. I hope there is something useful for at least someone reading this. 😅
2
u/blooping_blooper Jan 22 '26
These are ok to use if you are doing stuff live in a shell (if you like), but I would almost never use them in an actual script. Just like command aliases, you're trading away readability (maintainability) for minor conveniences.
2
u/faze_fazebook Jan 24 '26
Are you trying to learn powershell as your first programming language? Thats mental.
68
u/tokenathiest Jan 22 '26
These are not optimizations. These are control flow operators.
3
u/teethingrooster Jan 22 '26
Better to just write it in a way that makes it easy to know what is going on. If statements and they like.
Optimizing powershell can make sense for big data queries and such but for the most part when you need it it’s best to just drop down to c# where available.
3
u/stihlmental Jan 23 '26
Excellent post OP
5
u/nealfive Jan 22 '26
I would not call that optimization. Clean spelled out code is IMO better in most cases.
2
u/baron--greenback Jan 22 '26
What the heeelllll 👀
2
u/RyeonToast Jan 22 '26
the semi-colon doesn't actually change how the program flows, it just lets you put mutiple commands on one line. I use it a lot for testing because I can combine commands for resetting my test with the command that runs whatever I'm testing so I can rerun it by pressing up to find that line and enter to run it again.
I don't think I've every used the double-pipe or double-ampersands before. A good general rule is use whatever works and is easy to read. These might make sense for banging away on the console, but I usually just assign output to variables and check the output before proceeding.
1
u/VeryRareHuman Jan 22 '26
Great! I didn't know these operators exist.
you mean refractor your code with these operators? not worth it.
1
u/PutridLadder9192 Jan 23 '26
WARNING: && and || only work in pwsh 7
1
u/Rwinarch Jan 23 '26
Operators? These are not optimizations
-4
u/OlivTheFrog Jan 22 '26 edited Jan 23 '26
Salut,
Cordialement
Addendum : My Bad, I spoke for PS5.1 not for PS 7.x
-7
u/Mousse_Willing Jan 22 '26
Whatever copilot does. Use that.