0

I'm in the process of writing a PowerShell script to automatically decline certain updates in WSUS. Any Driver updates or language packs can be safely declined. For some reason, I got stuck wondering about this question.

The first is a standard If/ElseIf.

If  ( $update.UpdateClassificationTitle  -eq  'Drivers' )
{
    $update.Decline()
}
ElseIf  ( $update.UpdateTitle  -match  'LanguageFeature' )
{
    $update.Decline()
}

The second is if (true -or true).

If  (  ( $update.UpdateClassificationTitle  -eq  'Drivers' )  -or  ( $update.UpdateTitle  -match  'LanguageFeature' )  )
{
    $update.Decline()
}

Which is more efficient? How is each evaluated? Which is better practice?

  • 5
    If you're worrying about efficiency in a scripting language on the statement level, you're definitely worrying about the wrong thing. Worry about maintainability and readability instead. In that vein, I would actually use neither of these, but two separate `if`s (as there is no logical order between these conditions). They can be packed into a function that returns a `bool` if repeating the `$update.Decline()` is undesirable. – Jeroen Mostert Jun 17 '20 at 14:53
  • What you might want to know is that in the second if, the later condition `($update.UpdateTitle -match 'LanguageFeature')` is also not evaluated if the first condition `($update.UpdateClassificationTitle -eq 'Drivers') `is already `$True`. Based on this, the `-Match` operator is generally a little more expensive than the `-eq` operator, therefore I would check on the `-eq` operator first (as you did). – iRon Jun 18 '20 at 11:26

0 Answers0