Get SharePoint Online Group Information from PowerShell using Client Object Model


If you try to use PowerShell and CSOM to get SharePoint Online Group Owner information, you learn that the returned Microsoft.SharePoint.Client.Group object Owner property will be empty.  I tried to read value of Owner property and got the following.

image

I found two ways to get over this.  First I used the basic Object.Retreive method and pass an array or string properties.  The output was show as expected.

To see all the properties you can use

$Owner | Select *

$Group = $Web.SiteGroups.GetByName(“Test”)

$Owner = $Group.Owner

$Owner.Retrieve(“Id”,”LoginName”,”Title”,”PrincipalType”,”IsHiddenInUI”)

$Context.ExecuteQuery() $

$Owner | Select *

The output is exactly as I expected

image

The second method would be to use Lambda expressions.  PowerShell does not support lambda expressions like C# (object, a => a.XYZ) so I did some research and found a post from PowerShell Guru Garry Lapointe here.  It has a PowerShell script which enables you to call any property of the main object directly. 

https://gist.github.com/glapointe/cc75574a1d4a225f401b#file-load-csomproperties-ps1

$Group = $Web.SiteGroups.GetByName(“Test”)
$Owner = $Group.Owner
Load-CSOMProperties -object $Group.Owner -propertyNames @(“ID”,”LoginName”,”Title”,”PrincipalType”,”IsHiddenInUI”)
$Context.ExecuteQuery()
$Owner | Select *

Output is exactly the same but lot cleaner script as I don’t have to work with Retrieve method.
image