Forum Discussion

Joe Fischer's avatar
Joe Fischer
Brass Contributor
Jul 19, 2018
Solved

PowerShell script to get External users

Hi All,

I am new to PowerShell and I was wondering if anyone could help me with the the following code.

I get to the login but then there are no rows returned and I get error saving the CSV since there is no data.

I added three external users yesterday, so I know I have some :)

 

TIA,

Joe

 

#Specify tenant admin and site URL
$User = "joe@<company>.com"
$SiteURL = "https://<Domain>.sharepoint.com"


#Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path "C:\Program Files (x86)\Sharegate\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files (x86)\Sharegate\Microsoft.SharePoint.Client.Runtime.dll"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString

#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

try {
    for ($i=0;;$i+=50) {
        $ExternalUsers += Get-SPOExternalUser -SiteUrl $site.Url -PageSize 50 -Position $i -ea Stop | Select DisplayName,EMail,AcceptedAs,WhenCreated,InvitedBy,@{Name = "Url" ; Expression = { $site.url } }
    }
}
catch {
}

$ExternalUsers | Export-Csv -Path "C:\ExternalUsers.csv" -NoTypeInformation

 

 

  • Joe Fischer's avatar
    Joe Fischer
    Jul 20, 2018

    Newbie mistake.....

     

    I was just about to reply back to my post. 

    I was not using the "SharePoint Online Managment Shell" 

    When I ran, it complained it couildn't find the DLLs, so I search my PC for it and that's where they were.

     

    I installed the Management shell and all is good. 

     

    Thanks,

    Joe

2 Replies

    • Joe Fischer's avatar
      Joe Fischer
      Brass Contributor

      Newbie mistake.....

       

      I was just about to reply back to my post. 

      I was not using the "SharePoint Online Managment Shell" 

      When I ran, it complained it couildn't find the DLLs, so I search my PC for it and that's where they were.

       

      I installed the Management shell and all is good. 

       

      Thanks,

      Joe

Resources