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


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 = "admin@contoso.onmicrosoft.com"
$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 " "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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