I’m trying to get the « copy link » url from sharing documents on my sharepoint online.

enter image description here

How can i get this parameter with powershell script?

Thanks for your answer

2 Answers 2


Call CSOM method CreateAnonymousLink - link to Microsoft documentation.

Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  

$siteUrl = $null
$siteUrl = "https://contoso.sharepoint.com"

$username = "[email protected]"
$password = 'my awesome password' | ConvertTo-SecureString -asPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($username,$password)

Connect-PnPOnline –Url $siteUrl –Credentials $credential

$ctx = $null
$ctx = Get-PnPContext  

$web = $null
$web = Get-PnPWeb 
Write-Host "Web: " $web.Url

$list = $null
$list = Get-PnPList -Identity "Documents"
Write-host "List: " $list.Title

$item = Get-PnPListItem -List Documents -Id 18

Write-host "Item: " $item.Id " // " $item["FileLeafRef"]
$itemUrl = $web.Url  + $list.RootFolder.ServerRelativeUrl + "/" + $item["FileLeafRef"]

$ctxClient = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$ctxClient.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$password)

$link = [Microsoft.SharePoint.Client.Web]::CreateAnonymousLink($ctxClient,$itemUrl,$false)
Write-host "Link (view): "$link.Value

$link2 = [Microsoft.SharePoint.Client.Web]::CreateAnonymousLink($ctxClient,$itemUrl,$true)
Write-host "Link (edit): "$link2.Value

$ctx = $null

Write-Host "End" -f Green -b DarkGreen
Write-host " "
Write-host " "

Great post. I'd also like to add that at least for what I'm doing, there's no need to create a new ClientContext object. I was able to use the context from the $web object by referencing its context as $web.Context.

$link = [Microsoft.SharePoint.Client.Web]::CreateAnonymousLink($web.Context,$itemUrl,$false)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.