SharePoint External links audit

Dmytro Novitnii 0 Reputation points
2024-12-05T17:01:05.8233333+00:00

Good afternoon!

I have a task to create a PowerShell code to check all shared links from our SharePoint site, regardless of when they were created. The current limit is 180 days. Is it possible to use PowerShell to check for the entire period?

We need to close links that were shared more than 180 days ago.

Also, is it possible to also include the links created when a Guest user is invited to a MS Teams team (so granting also access to the Files tab)? Thank you in advance for your help.

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,154 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,708 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,481 Reputation points
    2024-12-05T21:12:28.2666667+00:00

    Posting the code you're using would be a good idea.

    I know nothing about SharePoint, but I posed the question to Microsoft's CoPilot and it gave me this code:

    $libraryName = "<your-library>"
    $items = Get-PnPListItem -List $libraryName -PageSize 1000
    $cutoffDate = (Get-Date).AddDays(-180)
    foreach ($item in $items) {
        $sharingLinks = Get-PnPFileSharingLink -Identity $item.Id
        foreach ($link in $sharingLinks) {
            if ($link.Scope -eq "External" -and $link.Created -lt $cutoffDate) {
                Write-Output "Item: $($item.FieldValues.Title), Link: $($link.Url), Created: $($link.Created)"
            }
        }
    }
    

    Now, whether that works or not, I don't know. I know that I'd change the code (a little bit) to

    $libraryName = "<your-library>"
    $cutoffDate = (Get-Date).AddDays(-180).Date
    $CsvFile = "C:\Temp\ExternalLinks.CSV"
    
    Get-PnPListItem -List $libraryName -PageSize 1000 |
        ForEach-Object |
            Get-PnPFileSharingLink -Identity $_.Id |
                ForEach-Object {
                    if ($_.Scope -eq "External" -and ($_.Created).Date -lt $cutoffDate) {
                        [PSObject]@{
                            Item = $_.FieldValues.Title
                            Link = $_.Url
                            Created = $_.Created
                        }
                    }
                } | Export-CSV $CsvFile -NoTypeInformation
    

    That same code will identify the links in your team sites library. The link for guests will be made in the "Documents" folder of the team site.

    0 comments No comments

  2. Yanli Jiang - MSFT 28,041 Reputation points Microsoft Vendor
    2024-12-06T08:57:25.6366667+00:00

    Hi @Dmytro Novitnii ,

    From the perspective of SharePoint, I found a blog that is similar to your needs, you can refer to it:

    https://o365reports.com/2024/07/16/get-all-expired-anyone-links-in-sharepoint-online-using-powershell/

    Please Note: Since the web site is not hosted by Microsoft, the link may change without notice. Microsoft does not guarantee the accuracy of this information.

    You can adjust the time judgment in the script according to your actual situation to meet your needs. Because there is no scenario that meets the conditions in my environment, there is no way to test it. I suggest you try it first, and we will discuss and modify it further if there are any problems.

    As a SharePoint engineer, I am not very good at Teams, so I suggest you wait for the relevant team to reply to you.

    Good day!


    If the answer is helpful, please click "Accept as Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.