Question about Best Practice – IfElse or Switch

Welcome Forums General PowerShell Q&A Question about Best Practice – IfElse or Switch

Viewing 4 reply threads
  • Author
    Posts
    • #245141
      Participant
      Topics: 5
      Replies: 10
      Points: 87
      Rank: Member

      Wanting to basically have “options” within a script. If a user is a member of a specific OU (based on their distinguished name) do this task. If they are a member of another OU, do a separate (but nearly identical thing).

      I’ve seen some mention using an ElseIf combination but I’ve seen elsewhere that people suggest using Switches. I’m honestly not super familiar with loops and even less so with switches. Anyone have any input/suggestions?

       

      etc..

      • This topic was modified 2 months ago by ALombardi01. Reason: typo
    • #245159
      Participant
      Topics: 15
      Replies: 1776
      Points: 3,218
      Helping Hand
      Rank: Community Hero

      If you are comparing the same variable, then you should use switch:

      Output:

      Personally rarely use ElseIf logic in Powershell.

    • #245162
      Participant
      Topics: 51
      Replies: 209
      Points: 860
      Helping Hand
      Rank: Major Contributor

      I like to use a Switch for this type of thing

      • This reply was modified 2 months ago by Iain.
    • #245168
      Participant
      Topics: 15
      Replies: 1776
      Points: 3,218
      Helping Hand
      Rank: Community Hero

      @Iain

      The script block is unnecessary as -eq is the default:

    • #245181
      Participant
      Topics: 5
      Replies: 10
      Points: 87
      Rank: Member

      Thanks to both of you, @Iain and @Rob

Viewing 4 reply threads
  • You must be logged in to reply to this topic.