What PowerShell can add an external user to a SharePoint Online Library or a document as a different user, so the inviter shows up as John Smith and not the user who is running the script?

frob 4,251 Reputation points
2022-07-29T15:33:53.847+00:00

Hi there
What PowerShell can add an external user to a SharePoint Online Library or a document as a different user, so the inviter shows up as John Smith and not the user who is running the script?
Thanks.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,230 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,628 questions
{count} votes

Accepted answer
  1. Tong Zhang_MSFT 9,251 Reputation points
    2022-08-01T07:02:07.443+00:00

    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.



0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.