SharePoint Online: PowerShell to Check If User Exists

Requirement: SharePoint Online PowerShell to check if a user exists.

PowerShell to check if the user exists in sharepoint online

There are many times when you need to check if a user exists in SharePoint Online. Maybe you’re trying to see if a new user account is still active, or perhaps you’re tracking down someone’s Email or Account’s validity in your SharePoint Online environment. PowerShell makes this process quick and easy, so let’s have a look at how it’s done. We will provide you with two methods that you can use to check the user’s existence. The first method uses the MSOnline PowerShell Module and the second method uses the SharePoint Online CSOM & PowerShell.

Option 1: Check If User Exists in the Tenant using Azure AD Cmdlet

There are different cases where you may need to check if a user exists in the Office 365 tenant, and here are my PowerShell scripts. We can use the Get-MsolUser cmdlet of Azure AD to check if a given user account is valid.

Import-Module MSOnline

#User Account Variable
$UserAccount = "Salaudeen@crescent.com"

Try {
    #Get Credentials to connect
    $Cred = Get-Credential
 
    #Connect to Azure AD
    Connect-MsolService -Credential $Cred

    #Function to check if a user account exists in the tenant
    Function Check-UserExists()
    {
        Param( [Parameter(Mandatory=$true)] [string]$UserID )
     
        $User = Get-Msoluser -UserPrincipalName $UserID -Erroraction SilentlyContinue
        if ($User -ne $null)
        {
            Return $True
        }
        else
        {
            Return $False
        }
    }
    #Call the function to validate the user account
    Check-UserExists $UserAccount
}
Catch {    
    write-host -f Red "Error:" $_.Exception.Message
}

Option 2: SharePoint Online Ensure User method to Validate a user

Here is the native SharePoint method to validate a user using PowerShell: The “EnsureUser” method.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Function to check if a user account is valid
Function Ensure-SPOUser()
{
    Param( 
        [Parameter(Mandatory=$true)] [string]$UserID,
        [Parameter(Mandatory=$true)] [string]$SiteURL
        )
    Try {
        #Setup Credentials to connect
        $Cred = Get-Credential

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
      
        #ensure sharepoint online user
        $Web = $Ctx.Web
        $User=$web.EnsureUser($UserID)
        $Ctx.ExecuteQuery()
        Return $True
    }
    Catch {    
        Return $False
    }
}

#Variables
$SiteURL = "https://crescent.sharepoint.com/Sites/marketing"
$UserID = "Salaudeen@crescent.com"
  
#Call the function to Check if the user account is valid
Ensure-SPOUser -UserID $UserID -SiteURL $SiteURL

These methods are helpful when you want to programmatically determine whether a user exists (or is orphaned) in the SharePoint and take appropriate action depending on the result.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *