Office 365: How to Get all Guests Users in an Office 365 tenant with PowerShell (II)!

This time I’m sharing a PowerShell script that allows to get all the Guests Users in an Office 365 teantn and export them to a CSV file using PowerShell for Azure AD (Note: You can check my previous article on this topic here).You can download the script from the following URL: How to get all guests users in an Office 365 tenant using PowerShell Azure AD

############################################################################################################################################
# Script that allows to get all the members of all the Guest Users in an Office 365 tenant by means of Azure AD PowerShell cmdlets
# Required Parameters:
#  ->sCSVFilenName: Name of the file to be generated
############################################################################################################################################

$host.Runspace.ThreadOptions = "ReuseThread"

#Definition of the function that get all the Guest Users in an Office 365 tenant through Azure AD PowerShell cmdlets and export the results to a CSV file
function Get-O365GuestsUsersThroughAzureAD
{   
    param($sCSVFileName) 
    Try
    {   
        [array]$O365GuestsUsers = $null
        $O365TenantGuestsUsers=Get-AzureADUser -Filter "Usertype eq 'Guest'”
        foreach ($O365GuestUser in $O365TenantGuestsUsers) 
        { 
            $O365GuestsUsers=New-Object PSObject
            $O365GuestsUsers | Add-Member NoteProperty -Name "Guest User DisplayName" -Value $O365GuestUser.DisplayName
            $O365GuestsUsers | Add-Member NoteProperty -Name "User Principal Name" -Value $O365GuestUser.UserPrincipalName
            $O365GuestsUsers | Add-Member NoteProperty -Name "Mail Address" -Value $O365GuestUser.Mail
            $O365AllGuestsUsers+=$O365GuestsUsers  
        } 
        $O365AllGuestsUsers | Export-Csv $sCSVFileName
    }
    catch [System.Exception]
    {
        Write-Host -ForegroundColor Red $_.Exception.ToString()   
    } 
}


#Connection to Office 365
$sUserName="<Your_Office365_Admin_Account>"
$sMessage="Introduce your Office 365 Credentials"
#Connection to Office 365
$O365Cred=Get-Credential -UserName $sUserName -Message $sMessage
#Creating an EXO PowerShell session
Connect-AzureAD -Credential $O365Cred -Confirm:$true


$sCSVFileName="AllTenantGuestsUsers.csv"
#Getting Tenant Guests Users
Get-O365GuestsUsersThroughAzureAD -sCSVFileName $sCSVFileNam

2 pensamientos en “Office 365: How to Get all Guests Users in an Office 365 tenant with PowerShell (II)!

  1. Pingback: SharePoint & Office 365: Resumen de posts (XXIII)! | Pasión por la tecnología...

  2. Pingback: SharePoint & Office 365: Compilation of blog posts (VIII)! | Pasión por la tecnología...

Deja una respuesta

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Salir /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Salir /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Salir /  Cambiar )

Conectando a %s