Most errors in Powershell are "Non-terminating" by default, that is, they do not cause your script to stop executing when they occur. Therefore, ls will be executed even after an error in the rm command.
However, you can change this behavior in several ways. You can change it globally using the $errorActionPreference variable (for example, $errorActionPreference = 'Stop' ) or change it only for a specific command by setting the -ErrorAction parameter, which is common to all cmdlets. This is the approach that makes the most sense to you.
# setting ErrorAction to Stop will cause all errors to be "Terminating"
Or using some common abbreviations
rm 'not-exists' -ea 1; ls
The -ErrorAction parameter -ErrorAction explained by help. Type Get-Help about_CommonParameters
source share