Forum Discussion

Hjb118's avatar
Hjb118
Copper Contributor
Mar 24, 2022
Solved

Powershell Get-ADuser if Statement

Good afternoon all Can I please have help with the following: Problem 1: In the image attached I am trying to get AD user attribute "mail" and see if it equal company's email address but for some...
  • farismalaeb's avatar
    farismalaeb
    Mar 25, 2022

    Hjb118 

    A small update here.

    $users=Import-Csv C:\Users\f.malaeb\Downloads\user1.csv
    
    foreach ($user in $users){
    #Here Checking if the user is exist
    if ((Get-ADUser -Filter "SamAccountName -eq '$($user.Username)'")){
    write-host $($user).username "is exist, Lets check the email" -ForegroundColor Green   
    #If the user exist Check if the Email is exist
        if (Get-Mailbox $user.Username -ErrorAction SilentlyContinue){
        #Here will create a mailbox for this user as its exist in AD but not in Exchange
        Write-Host $User.Username "Already have an email" -ForegroundColor Green
            
        }
        Else{ Write-Host "Creating a New mailbox for" $User.Username -ForegroundColor Yellow}
    }
    Else{
    Write-Host $($User).Username "is NOT exists" -ForegroundColor Red}
    }
    
    

     

    The script will:

    - Check if the user exists or not

    - If a user exists, then and as you already got an active exchange session, use it to check if the user have a mailbox.

    - if dont , then create a mailbox

    -if the user is not exist then create the user.

     

Resources