Powershell Script to Remove Sharing Links After 90 Days

SharePoint Asked by Sylvie on February 17, 2021

I’d like to know if it’s possible to create a script to check a library for all guest access links (authenticated, not anonymous) and if they are older than 90 days, remove them.
So for each folder that was shared with an external user <90 days ago, remove link.

Here is a script to remove all links (tweaked from this post Remove a specific link from SharePoint Online):

Function Remove-SPSharingLink { 

param (    

    Connect-PnPOnline -Url $SPURL  
    $Ctx= Get-PnPContext 

    $Files= Get-PnPListItem -List "Documents" 
    foreach( $File in $Files) 

            $Froles= $File.RoleAssignments 

            If($Froles.Count -gt 0) 

              for ($i = $Froles.Count -1; $i -ge 0 ; --$i)   
                  If($Link.title -like "SharingLinks*") 
                  $Link = $null 


Remove-SPSharingLink -SPURL “

But I’m not sure how to get the sharing link details and filter by date.


One Answer

There is no way, that I am aware of, to get the link shared date. You could store it as part of a script or app but that won't help for the first time you run it.

You can find guests using a search query and appending ViewableByExternalUsers=true. It is less expensive and quicker than what you are trying. So something like:

#This checks the last modified date. Not the link date...
#The important part is ViewableByExternalUsers:true
#You can figure this query out to suit you from a search box. 
#This only returns docs. You may want folders etc.
$query = "Write>=$((Get-date).AddDays(-90).ToString("dd/MM/yyyy")) AND ViewableByExternalUsers:true AND IsDocument:true AND NOT contentclass:STS_ListItem_WebPageLibrary"

$keyWordQry = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($ctx)
$keyWordQry.TrimDuplicates = $false
$keyWordQry.EnableStemming = $false
$keyWordQry.QueryText = $query
$searchExec = New-Object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($ctx)
$results = $searchExec.ExecuteQuery($keyWordQry)

At this point you have an object you can enumerate to get what you want:


Do note that this only returns the first 50 results by default. You can return more by setting $keyWordQry.RowLimit and/or passing a start point to the query via $keyWordQry.StartRow

Also you would need to handle folder, library and site sharing. All possible with search though.

Answered by Bunzab on February 17, 2021

Add your own answers!

Related Questions

Is there an API or web service for Copy-SPSite?

0  Asked on October 25, 2021 by spdevadmin


Calculated Fields and Outputting to Excel

1  Asked on February 28, 2021 by brandon


PnP with .NET Core

1  Asked on February 23, 2021 by thomas-segato


How can I use MS Web Test to upload files to SharePoint?

2  Asked on February 19, 2021 by user1258


Filtering List View by Current User Email

2  Asked on February 18, 2021 by hushhoney


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP