Hi @frob ,
Do you want to share the a document library or a file with external users? If my understanding is incorrect, please feel free to contact me.
If yes, you can use the following script to share the document to external user:
#Load required SharePoint client assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
#Specify SharePoint Online Site URL
$SiteURL = "https://contoso.sharepoint.com/sites/site_name"
#Set full path (URL) of the document to be shared.
$ItemUrl = "https://contoso.sharepoint.com/sites/site_name/document_library/testfile.docx"
#Get user credentials to connect
$Cred = Get-Credential
#User to share the resource
$User = "******@domain.com"
# peoplePickerInput object as json string
$PeoplePickerInput = "[{'Key' : '$($User)', 'Description' : '$($User)', 'DisplayText' : '$($User)', 'EntityType' : 'User', 'ProviderDisplayName' : 'Tenant', 'ProviderName' : 'Tenant', 'IsResolved' : false, 'EntityData' : {'Title' : '', 'MobilePhone' : '', 'Department' : '', 'Email' : '$($User)'}, 'MultipleMatches' : []}]"
#Set permission/role for the shared user. View = 1073741826, Edit = 1073741827
$RoleValue = "role:1073741826"
$SendEmail = $true
$IncludedAnonymousLinkInEmail = $false
$GroupId = 0
$UseSimplifiedRoles = $false
#A flag to determine if permissions should be pushed to items with unique permissions.
$PropageAcl =$false
$EmailSubject = "Site Shared"
$EmailBody = "Please accept this invitation to access the TestDocument file"
#Setup SharePoint site context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Share document with external users
$Result = [Microsoft.SharePoint.Client.Web]::ShareObject($Ctx,$ItemUrl,$PeoplePickerInput,$RoleValue,$Groupid,$PropageAcl,$SendEmail,$IncludedAnonymousLinkInEmail,$EmailSubject,$EmailBody,$UseSimplifiedRoles)
$Ctx.Load($Result)
$Ctx.ExecuteQuery()
Write-Host "Status code :$($Result.StatusCode)"
if($Result.ErrorMessage)
{
Write-Host "ErrorMessage :$($Result.ErrorMessage)" f Red
}
If you want to add an external user to the SharePoint Online site, you can use the following script:
#Parameters
$AdminCenterURL="https://Crescent-admin.sharepoint.com"
#Setup Credentials to connect
Connect-SPOService -Url $AdminCenterURL -Credential (Get-Credential)
#add guest user to sharepoint online site
Add-SPOUser -Group "Marketing Visitors" -LoginName "******@NationalAquarium.com" -Site "https://Crescent.sharepoint.com/sites/Marketing"
More information for reference:
SharePoint Online: How to Add External User using PowerShell?
Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.