Get-SPOUserProfile.ps1



function Get-SPOUserProfiles {
    [CmdletBinding(DefaultParameterSetName="Context")]
    Param(
        [Parameter(ParameterSetName="Context",Mandatory=$true,ValueFromPipeline)]
        [Microsoft.SharePoint.Client.ClientRuntimeContext[]]
        $Context = $Global:SPOCSOMContext,

        [Parameter(ParameterSetName="Context",Mandatory=$true,ValueFromPipeline)]
        [Microsoft.SharePoint.Client.User[]]
        $Users

        #[Parameter(ParameterSetName="web",Mandatory=$True,ValueFromPipeline)]
        #[Microsoft.SharePoint.Client.Web[]]
        #$Web
    )

    If(-Not(Get-Module Microsoft.SharePoint.Client.UserProfiles)){
        if(Test-Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\"){
            $subdirs = Get-ChildItem "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\" | Where-Object {$_.Name -Match "\d{2}"} | Sort-Object -Descending
            Write-Verbose "Found $($subdirs.Count) possible location(s) for the the Microsoft.SharePoint.Client.UserProfiles.dll module"
            $subdirs | ForEach-Object {
                if(Test-Path ($_.FullName + "\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll")){
                    Write-Verbose "Loading $($_.FullName)\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"
                    Import-Module ($_.FullName + "\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll") -ErrorAction Stop
                    Break
                }

            }
        }
        if(-Not(Get-Module Microsoft.SharePoint.Client.UserProfiles)){
            Throw "Please make sure the SharePoint Client Components SDK is installed `n 2016: https://www.microsoft.com/en-us/download/details.aspx?id=51679 `n 2013: http://www.microsoft.com/en-us/download/details.aspx?id=35585"
        }
    }

    [Microsoft.SharePoint.Client.UserProfiles.PersonProperties[]]$result = @()

    $PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Context)
    Foreach($u in $users){
        $UserProfile = $PeopleManager.GetPropertiesFor($u.LoginName)
        $result += Initialize-SPOCSOMCollections -CSOMCollection $UserProfile -ReturnObject
    }
    return ,$result
}

#$SPONonSyncedUsers = $userprofiles | ? {$_.ExtendedManagers -match "."} #
#$SPONonSyncedUsers | select AccountName,DisplayName,Email,@{L="Manager";E={$_.ExtendedManagers[($_.ExtendedManagers.Count - 1)]}} | Export-Csv -NoTypeInformation NonSyncedSPO-AADUsers.csv
#$u = $userprofiles[0..10] | ? {$_.Email -eq 'sgackerman@geisinger.onmicrosoft.com'}