List All the Users in a Site
How do I list all the users with accounts in a particular site? Hmmm, that is an interesting question: how do you list all the users who have accounts in a particular site?
Note. Not that we wish to imply that other questions aren’t interesting; as we all know, there are no dumb questions.
Although if you’ve ever had to sit through a company meeting you might wonder about that.
As it turns out, there’s no straightforward way to answer the question: the Get-CsUser cmdlet doesn’t tell you which site a user belongs to, and the Get-CsSite cmdlet doesn’t tell you which users have accounts in that site. But, with a little master detective work, and a little PowerShell scripting, we can come up with an answer:
$pools = (Get-CsSite -Identity $args[0]).Pools
foreach ($pool in $pools)
{
$pool
$users = (Get-CsUser | Where-Object {$_.RegistrarPool -like $pool})
foreach ($user in $users)
{
$user.DisplayName
}
Write-Host
}
So what kind of detective work are we doing here? Well, as it turns out, what the Get-CsSite cmdlet does return is a list of all the pools assigned to that site. In our first command, we simply return the Identity of each pool in a given site. (The variable $args[0] represents the site name; $args[0] is PowerShell-speak for the first command-line argument passed to the script.) After we have the pool names we then set up a foreach loop that cycles through all the pools in the collection, using the Get-CsUser cmdlet to return the DisplayName of any user who is homed on that pool. (The RegistrarPool property tells us which pool a user account is homed on.) The net effect? We get back a list of all the users who have accounts in a given site.
So is this the way that Sherlock Holmes or Columbo or Monk or any of the other great detectives in history (even the Great Mouse Detective?) would have solved this problem? You bet; in fact, we’re pretty sure that Sherlock Holmes solved all his cases using Windows PowerShell.
Not 100% sure, mind you. But pretty sure.
By the way, to use this script simply copy the code and paste it into your favorite text editor. Save the file with a .ps1 file extension (e.g., C:\Scripts\UsersBySite.ps1) and then call the script from within the Lync Server Management Shell:
C:\Scripts\UsersBySite.ps1 "Redmond"
As you can see, the script requires only one command-line argument: the name of the site (Redmond).