In this article I will present to you a small script that lets us get a list of all  users from a specific Office 365 Tenant.

The Script:

Table of Contents
    Import-Module AzureAutomationAuthoringToolkit
    $username = "migadm@titanbuckle.com"
    $password = ConvertTo-SecureString "Muva9659" -AsPlainText -Force
    $creds = New-Object System.Management.Automation.PSCredential($username, $password)
    Connect-MsolService -Credential $creds
    $users = Get-MsolUser
    
    ForEach ($user in $users) {
        $SignInName = $user.SignInName
        $Country = $user.Country
        $DisplayName = $user.DisplayName
        $FirstName = $user.FirstName
        $LastName = $user.Tee
        $IsLicensed = $user.IsLicensed
        $PreferredLanguage = $user.PreferredLanguage
        $SignInName = $user.SignInName
        $UsageLocation = $user.UsageLocation
        $UserPrincipalName = $user.UserPrincipalName
        $UserType = $user.UserType
    }

    Now let us go through the script and see what every line does.

    First, we Import the AzureAutomationAuthoringToolkit that will provide us with the necessary cmdlets.

    Next, the $username variable holds the username of a Global Administrator of the Office 365 tenant. Afterwards, the $password variable holds the password of that specific user, which in turn, is passed as a Secure String to the login process.

    On the fourth line, we create the $creds variable that holds the PowerShell Credentials object.

    Then, we use the Connect-MSOLService cmdlet to connect to the specific Office 365 tenant that we want to pull the info from.

    Once connected, we create a variable called $users that runs the Get-MsolUser command. Once the command is run, the variable becomes an array that will hold the output.

    Now that we have all the info that we need, let’s output the data.

    For that we create a ForEach loop where we output each element in the array, or each user, and list the SignInName, Country, DisplayName, FirstName, IsLicensed fields and so forth.

    That’s it! Simple, right?