Forum Discussion
PowerShell Script to get Share URLs for folders in a SharePoint Library
Hi all. My org has created a site/document library in our SPO instance to share files with external collaborators. Does anyone know if it's possible to create a PowerShell script that will generate a list of the Share URLs for each folder in that document library?
- AlainBaillyCopper Contributor
Mcorbett
Hello, i think this code Will help you :#Parameters
$SiteUrl = "https://your sharepoint site"
$ReportOutput = "C:\Temp\SharedLinks.csv"
$ListName = "Libraryname"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
$Ctx = Get-PnPContext
$Results = @()
$global:counter = 0
#Get all list items in batches
$ListItems = Get-PnPListItem -List $ListName -PageSize 2000
$ItemCount = $ListItems.Count
#Iterate through each list item
ForEach($Item in $ListItems)
{
Write-Progress -PercentComplete ($global:Counter / ($ItemCount) * 100) -Activity "Getting Shared Links from '$($Item.FieldValues["FileRef"])'" -Status "Processing Items $global:Counter to $($ItemCount)";
#Get Shared Links
$SharingInfo = [Microsoft.SharePoint.Client.ObjectSharingInformation]::GetObjectSharingInformation($Ctx, $Item, $false, $false, $false, $true, $true, $true, $true)
$ctx.Load($SharingInfo)
$ctx.ExecuteQuery()
ForEach($ShareLink in $SharingInfo.SharingLinks)
{
If($ShareLink.Url)
{
If($ShareLink.IsEditLink)
{
$AccessType="Edit"
}
ElseIf($shareLink.IsReviewLink)
{
$AccessType="Review"
}
Else
{
$AccessType="ViewOnly"
}
#Collect the data
$Results += New-Object PSObject -property $([ordered]@{
Name = $Item.FieldValues["FileLeafRef"]
RelativeURL = $Item.FieldValues["FileRef"]
FileType = $Item.FieldValues["File_x0020_Type"]
ShareLink = $ShareLink.Url
ShareLinkAccess = $AccessType
ShareLinkType = $ShareLink.LinkKind
AllowsAnonymousAccess = $ShareLink.AllowsAnonymousAccess
IsActive = $ShareLink.IsActive
Expiration = $ShareLink.Expiration
})
}
}
$global:counter++
}
$Results | Export-CSV $ReportOutput -NoTypeInformation
Write-host -f Green "Sharing Links Report Generated Successfully!"- adrien_itrCopper Contributor
Hello !
Thanks for this answer. I'm trying to get the same report, but I would like to add the identity of people to whom the link has been shared. I'm trying to use this code, added in the foreach loop :
$RoleAssignments = $ShareLink.RoleAssignments
foreach($ra in $RoleAssignments)
{
Write-host "$ra.Member.PrincipalType"
}
But the result is always null.
Do you have any idea how to get this information ? Do you know how to retrieve the identity of the users to whom the link has been shared with ?
Thanks !