powershell if statement to include more conditions

Eaven HUANG 2,191 Reputation points
2022-10-12T03:13:56.07+00:00

Dear all,

I'm trying to use following if statement to verify when user's job title contains the keyword of teacher, or visiting, or lecturer, or teaching fellow, or editor, then we will add some attributes for this users. however, the syntax i used below didn't actually take effect, what am I missing?

Thanks.

elseif ($jobtitle.ToLower().Contains("teacher" -or "visiting" -or "lecturer" -or "teaching fellow" -or 'Editor'))  
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. rafalzak 3,251 Reputation points
    2022-10-12T06:40:11.76+00:00

    Hi @Eaven HUANG ,

    I think you have answer in your previous question.
    Try something like this:
    elseif (($jobtitle.ToLower().Contains("teacher")) -OR ($jobtitle.ToLower().Contains("visiting")) -OR ($jobtitle.ToLower().Contains("lecturer")))

    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2022-10-12T15:40:30.37+00:00

    Here are two ways to approach the problem:

    $jobtitle = 'teaching fellow'  
      
    # this might be easier to maintain:  
    $found = $false  
    Switch -regex ($jobtitle) {  
            '\bteacher\b'           {$found = $true; break}  
            '\bvisiting\b'          {$found = $true; break}  
            '\blecturer\b'          {$found = $true; break}  
            '\bteaching fellow\b'   {$found = $true; break}  
            '\beditor\b'            {$found = $true; break}  
            default                 {$found = $false}  
         }  
    if ($found){  
        "found it"  
    }  
    else{  
        "not there"  
    }  
      
    #  
    # this is more compact, but harder to read and my become unwieldy if the number of choices increases  
    if ($jobtitle -match '\bteacher|visiting|lecturer|teaching\sfellow|editor\b'){  
        "found it"  
    }  
    else{  
        "not there"  
    }  
    

    In the string class, you can search for a string within a string, but you can't provide alternate choices: system.string.contains

    0 comments No comments

  3. Limitless Technology 44,746 Reputation points
    2022-10-13T07:52:54.86+00:00

    Hello there,

    You can nest blocks of if/else statements, depending on your particular use case.

    If you write you if/else block in a single line, then PowerShell won't bark at you. To nest if/else statements you must use the keyword elseif, which must have a condition specified (unlike a flat else block which directly contains the code to execute).

    You can have a quick look into this thread https://social.technet.microsoft.com/Forums/windowsserver/en-US/f34920d1-8d94-4d47-ba0f-791ed11a6d56/if-with-multiple-conditions?forum=winserverpowershell

    ---------------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.