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: 6
      Replies: 13
      Points: 100
      Rank: Participant

      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 5 months, 4 weeks ago by ALombardi01. Reason: typo
    • #245159
      Participant
      Topics: 17
      Replies: 1956
      Points: 4,010
      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: 52
      Replies: 211
      Points: 890
      Helping Hand
      Rank: Major Contributor

      I like to use a Switch for this type of thing

      • This reply was modified 5 months, 4 weeks ago by Iain.
    • #245168
      Participant
      Topics: 17
      Replies: 1956
      Points: 4,010
      Helping Hand
      Rank: Community Hero

      @Iain

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

    • #245181
      Participant
      Topics: 6
      Replies: 13
      Points: 100
      Rank: Participant

      Thanks to both of you, @Iain and @Rob

Viewing 4 reply threads
  • The topic ‘Question about Best Practice – IfElse or Switch’ is closed to new replies.