3

How can I list all the users from an office 365 account. I have tried this:

$users = $Context.web.SiteUsers;
                $userinfos = $Context.web.SiteUserInfoList;
                $Context.Load($users);
                $Context.Load($userinfos);


                $Context.ExecuteQuery();
                $isuser = $false

                foreach ($user in $users)
                {
                    write-host $user.LoginName

                }

However this does not work? Please help

1 Answer 1

3

Retrieving Office 365 users and SharePoint users are slightly different things.

With the CSOM method you describe, you will only be able to get SharePoint users who have accessed that site, or who have been explicitly added to groups on it etc.

If you want to get all of the Office 365 users in your tenancy, you need to use the Office 365 management cmdlets (https://support.office.com/en-us/article/Managing-Office-365-and-Exchange-Online-with-Windows-PowerShell-06a743bb-ceb6-49a9-a61d-db4ffdf54fa6)

Once installed, there is a Get-MsolUser cmdlet that will do what you need. There's various ways you can filter the users you want, and by default there's a limit on how many are returned, I think it's 5,000 users. If you simply ant all users, then the command to run is "Get-MsolUser -All".

3
  • 1
    There doesn't appear to be a limit on Get-MsolUser, I regularly retrieve in excess of 10k users with it. Commented Mar 21, 2016 at 9:53
  • Without the "-All" flag? The limit may have been removed or increased, I used to need the "-All" flag at least (for approx 15k users) Commented Mar 22, 2016 at 10:20
  • You are correct, I misread. In fact I think that if you have more than the limit you get either a warning or no results. You can also use -MaxResults of course. Commented Mar 22, 2016 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.