SharePoint Online - Check If The User Is Active

Introduction

We have many ways to check if a user is active or not. But I found using Azure AD PowerShell module is easy so that you do not need to connect to SPO module and getting the context. The use case here is if you have a list of users and want to check if the user is active or not, we can use Azure AD PowerShell module to check if the user is orphaned or active. In the references section, you can find out the other ways.

Prerequisites

  • Account having read access to organization Azure AD.
  • Azure AD Powershell module installed.

Steps

Step 1

Connect to Azure AD Module

Step 2

Import the CSV file

Step 3

For each record in CSV run the below Azure AD module command to check if the user exists or not. If the variable is empty, that means there is no information about the user. The parameter -ErrorAction Silently Continue, will continue to execute to next record if the error exisits. If there is an error, the variable $UserAccount will be NULL. If there is no error the $UserAccount will have the value. 

$UserAccount = Get-AzureADUser -Filter "userPrincipalName eq '[email protected]'" -ErrorAction SilentlyContinue 

Step 4

If the resultant string is empty then declare a status variable to say ‘Not Exists’. If the resultant string contains value then declare the status variable to say ‘Exisits’.

#Checking
if the user is active
If($UserInfo - eq $null) {
    Write - Host "'$email' doesn't in tenant" - ForegroundColor Yellow
    $UserStatus = "Not Exists"
}
Else {
    Write - Host "'$email' exists in tenant" - ForegroundColor Green
    $UserStatus = "Exists"
}

Complete Script

#Step1: Connect Azure AD if not connected already. No need for VPN
connect-AzureAD
#step2: Creating Output file to look into results
$currentTime = $(Get-Date).ToString("yyyymmddhhmmss")
$outputFile = "C:\Output\UserStatus-"+$currentTime+".csv";
#Step3: Import the CSV file that has all user emails
Add-Content -Path $outputFile -Value "Email,AccountStatus"
#Give the path to input csv file where all the user emails are located. Here i am giving as C:\Input
$GuestUsers=Import-Csv -Path "C:\Input\2021\Reviewers Contacts.csv"
foreach($email in ($GuestUsers.EmailAddress))
{
    $UserInfo=Get-AzureADUser -Filter "Mail eq '$email'" 
    #Checking if the user is active
    If($UserInfo -eq $null){
    Write-Host "'$email' doesn't exist in tenant" -ForegroundColor Yellow
    $UserStatus = "Not Exists"
    }
    Else{
    Write-Host "'$email' exists in tenant" -ForegroundColor Green
    $UserStatus = "Exists"
    }
    Add-Content -Path $outputFile -Value "'$email',$UserStatus"
}

When prompted sign with your organization account that has read access to Azure AD.

File Format

The input CSV should contain all the emails with the header value EmailAddress. Below is the screen capture for reference.

Output file

In the script the output file is generated in C:\OutPut. The output file will be in the following format.

Conclusion

Thus, in this article, we have seen how we can use Azure AD module to check the user is orphaned or active.

References


Similar Articles