Skip to content

Commit 604f8ae

Browse files
authored
Merge pull request #795 from reshmee011/deletenondirectsharinglink
sample script to delete company wide and anonymous link
2 parents 1d1893c + 2a2c162 commit 604f8ae

File tree

3 files changed

+207
-0
lines changed

3 files changed

+207
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
---
2+
plugin: add-to-gallery
3+
---
4+
5+
# Deletes company-wide and anonymous sharing links
6+
7+
## Summary
8+
9+
Sharing links can lead to oversharing, especially when default site sharing settings haven’t been updated to ‘People with existing access’. If default sharing options are not updated in the tenant or site, end users can easily create a company wide or anonymous sharing links with a single click. End users can make a conscious decision to create sharing links with specific "people you choose" which limits the audience having access to the data. This script can help delete those company-wide and anonymous sharing links at folder, file, and item levels. This approach can help mitigate oversharing issues during the M365 Copilot rollout.
10+
11+
![Example Screenshot](assets/preview.png)
12+
13+
### Prerequisites
14+
15+
- The user account that runs the script must have at least site owner role to the SharePoint Online site.
16+
17+
# [PnP PowerShell](#tab/pnpps)
18+
19+
```powershell
20+
param(
21+
[Parameter(Mandatory)]
22+
[ValidateSet('Yes','No')]
23+
[string]$ExcludeDirectSharingLink,#anonymous and company wide links are included by default and this provides an option to include the "People You choose" links in the deletio process.
24+
[Parameter(Mandatory)]
25+
[ValidateSet('Yes','No')]
26+
[string]$DeleteSharingink,
27+
[Parameter(Mandatory)]
28+
[string]$SiteUrl
29+
)
30+
31+
#Parameters
32+
$dateTime = (Get-Date).toString("dd-MM-yyyy-hh-ss")
33+
$invocation = (Get-Variable MyInvocation).Value
34+
$directorypath = Split-Path $invocation.MyCommand.Path
35+
$fileName = "SharedLinksDeletion-" + $dateTime + ".csv"
36+
$ReportOutput = $directorypath + "\Logs\"+ $fileName
37+
38+
$global:Results = @();
39+
40+
$siteUrl = if ($siteUrl[-1] -ne '/') { $siteUrl + '/' } else { $siteUrl }
41+
function getSharingLink($_object,$_type,$_siteUrl,$_listUrl)
42+
{
43+
$relativeUrl = $_object.FileRef
44+
$SharingLinks = if ($_type -eq 0 ) {
45+
Get-PnPFileSharingLink -Identity $relativeUrl
46+
} elseif ($_type -eq 1) {
47+
Get-PnPFolderSharingLink -Folder $relativeUrl
48+
}
49+
50+
ForEach($ShareLink in $SharingLinks)
51+
{
52+
if(($ExcludeDirectSharingLink -eq 'Yes' -and $ShareLink.Link.Scope -ne 'Users') -or $ExcludeDirectSharingLink -eq 'No')
53+
{
54+
$result = New-Object PSObject -property $([ordered]@{
55+
SiteUrl = $_SiteURL
56+
listUrl = $_listUrl
57+
Name = $_object.FileLeafRef
58+
RelativeURL = $_object.FileRef
59+
ObjectType = $_Type -eq 1 ? 'Folder':'File'
60+
ShareId = $ShareLink.Id
61+
RoleList = $ShareLink.Roles -join "|"
62+
Users = $ShareLink.GrantedToIdentitiesV2.User.Email -join "|"
63+
ShareLinkUrl = $ShareLink.Link.WebUrl
64+
ShareLinkType = $ShareLink.Link.Type
65+
ShareLinkScope = $ShareLink.Link.Scope
66+
Expiration = $ShareLink.ExpirationDateTime
67+
BlocksDownload = $ShareLink.Link.PreventsDowload
68+
RequiresPassword = $ShareLink.HasPassword
69+
})
70+
71+
$global:Results +=$result;
72+
73+
if($DeleteSharingink -eq 'Yes'){
74+
if ($_type -eq 0 ) {
75+
Remove-PnPFileSharingLink -FileUrl $relativeUrl -Identity $ShareLink.Id -Force
76+
} elseif ($_type -eq 1) {
77+
Remove-PnPFolderSharingLink -Folder $relativeUrl -Identity $ShareLink.Id -Force
78+
}
79+
}
80+
}
81+
}
82+
}
83+
#Exclude certain libraries/lists
84+
$ExcludedLists = @("Access Requests", "App Packages", "appdata", "appfiles","Apps for SharePoint" ,"Apps in Testing", "Cache Profiles", "Composed Looks", "Content and Structure Reports", "Content type publishing error log", "Converted Forms",
85+
"Device Channels", "Form Templates", "fpdatasources", "Get started with Apps for Office and SharePoint", "List Template Gallery", "Long Running Operation Status", "Maintenance Log Library", "Images", "site collection images"
86+
, "Master Docs", "Master Page Gallery", "MicroFeed", "NintexFormXml", "Quick Deploy Items", "Relationships List", "Reusable Content", "Reporting Metadata", "Reporting Templates", "Search Config List", "Site Assets", "Preservation Hold Library",
87+
"Site Pages", "Solution Gallery", "Style Library", "Suggested Content Browser Locations", "Theme Gallery", "TaxonomyHiddenList", "User Information List", "Web Part Gallery", "wfpub", "wfsvc", "Workflow History", "Workflow Tasks", "Pages")
88+
89+
Connect-PnPOnline -Url $siteUrl -Interactive
90+
91+
Write-Host "Processing site $siteUrl" -Foregroundcolor "Red";
92+
$ll = Get-PnPList -Includes BaseType, Hidden, Title,HasUniqueRoleAssignments,RootFolder | Where-Object {$_.Hidden -eq $False -and $_.Title -notin $ExcludedLists } #$_.BaseType -eq "DocumentLibrary"
93+
Write-Host "Number of lists $($ll.Count)";
94+
95+
foreach($list in $ll)
96+
{
97+
$listUrl = $list.RootFolder.ServerRelativeUrl;
98+
99+
$selectFields = "ID,HasUniqueRoleAssignments,FileRef,FileLeafRef,FileSystemObjectType"
100+
101+
$Url = $siteUrl + '_api/web/lists/getbytitle(''' + $($list.Title) + ''')/items?$select=' + $($selectFields)
102+
$nextLink = $Url
103+
$ListItems = @()
104+
while($nextLink){
105+
$response = invoke-pnpsprestmethod -Url $nextLink -Method Get
106+
107+
$ListItems += $response.value | where-object{$_.HasUniqueRoleAssignments -eq $true}
108+
if($response.'odata.nextlink'){
109+
$nextLink = $response.'odata.nextlink' -replace "$siteUrl/_api/",""
110+
} else{
111+
$nextLink = $null
112+
}
113+
}
114+
ForEach($item in $ListItems)
115+
{
116+
if($list.BaseType -eq "DocumentLibrary")
117+
{
118+
$type= $item.FileSystemObjectType;
119+
}
120+
121+
getSharingLink $item $type $siteUrl $listUrl;
122+
}
123+
}
124+
125+
$global:Results | Export-CSV $ReportOutput -NoTypeInformation
126+
Write-host -f Green "Sharing Links Report Generated Successfully! to $ReportOutput"
127+
```
128+
129+
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)]
130+
131+
***
132+
133+
## Source Credit
134+
135+
Sample first appeared on [Deletion of company-wide and anonymous sharing links with PowerShell](https://reshmeeauckloo.com/posts/powershell-delete-nondirectlink//)
136+
137+
## Contributors
138+
139+
| Author(s) |
140+
|-----------|
141+
| [Reshmee Auckloo](https://github.com/reshmee011) |
142+
143+
144+
[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)]
145+
<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-delete-companywide-anonymous-sharinglink" aria-hidden="true" />
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[
2+
{
3+
"name": "spo-delete-companywide-anonymous-sharinglink",
4+
"source": "pnp",
5+
"title": "Deletes company-wide and anonymous sharing links",
6+
"shortDescription": "Deletes company-wide and anonymous sharing links for folder, file, and item. This can address oversharing issues during the M365 Copilot rollout.",
7+
"url": "https://pnp.github.io/script-samples/spo-delete-companywide-anonymous-sharinglink/README.html",
8+
"longDescription": [
9+
"Sharing links can lead to oversharing, especially when default site sharing settings haven't been updated to 'People with existing access'. If default sharing options are not updated in the tenant or site, end users can easily create a company wide or anonymous sharing links with a single click. End users can make a conscious decision to create sharing links with specific 'people you choose' which limits the audience having access to the data. This script can help delete those company-wide and anonymous sharing links at folder, file, and item levels. This approach can help mitigate oversharing issues during the M365 Copilot rollout."
10+
],
11+
"creationDateTime": "2025-01-03",
12+
"updateDateTime": "2025-01-03",
13+
"products": [
14+
"SharePoint",
15+
"Sharing Links",
16+
"Microsoft 365 Copilot"
17+
],
18+
"metadata": [
19+
{
20+
"key": "PNP-POWERSHELL",
21+
"value": "2.99.90"
22+
}
23+
],
24+
"categories": [
25+
"Report",
26+
"Microsoft 365 Copilot"
27+
],
28+
"tags": [
29+
"modern",
30+
"Connect-PnPOnline",
31+
"Get-PnPFileSharingLink",
32+
"Get-PnPFolderSharingLink",
33+
"Get-PnPList",
34+
"Remove-PnPFileSharingLink",
35+
"Remove-PnPFolderSharingLink",
36+
"invoke-pnpsprestmethod"
37+
],
38+
"thumbnails": [
39+
{
40+
"type": "image",
41+
"order": 100,
42+
"url": "https://raw.githubusercontent.com/pnp/script-samples/main/scripts/spo-delete-companywide-anonymous-sharinglink/assets/preview.png",
43+
"alt": ""
44+
}
45+
],
46+
"authors": [
47+
{
48+
"gitHubAccount": "reshmee011",
49+
"company": "",
50+
"pictureUrl": "https://avatars.githubusercontent.com/u/7693852?v=4",
51+
"name": "Reshmee Auckloo"
52+
}
53+
],
54+
"references": [
55+
{
56+
"name": "Want to learn more about PnP PowerShell and the cmdlets",
57+
"description": "Check out the PnP PowerShell site to get started and for the reference to the cmdlets.",
58+
"url": "https://aka.ms/pnp/powershell"
59+
}
60+
]
61+
}
62+
]

0 commit comments

Comments
 (0)