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?
View all comments
Show parent comment
2
u/Head-Ad-3063 Jan 22 '26
I've only come across a couple of times when you really need to optimise powershell, it's not generally a time critical thing, it's scripting, not programming.
The main one when I had to really speed up powershell was a subversion backup script when I had to use PS7 so I could multithread it.