external users – Powershell Script to Remove Sharing Links After 90 Days

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.