Enable Anonymous Access in SharePoint using PowerShell

How to Enable Anonymous Access in SharePoint using PowerShell?

Enabling anonymous access in SharePoint allows users who are not logged into the system to view and interact with content. Continuing with my last post, How to Enable Anonymous Access in SharePoint 2013? In this blog post, we’ll show you how to enable anonymous access in SharePoint using PowerShell. Here are some nifty PowerShell scripts to manage anonymous access in SharePoint:

Enable Anonymous Access in SharePoint using PowerShell

Enabling anonymous access in SharePoint involves three steps:

  1. Enable anonymous access for SharePoint web application
  2. Enable anonymous access to SharePoint site
  3. Turn ON anonymous access on lists and libraries

PowerShell to Enable Anonymous Access settings of a Web Application:

Add-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$webApp = Get-SPWebApplication 'https://sharepoint.crescent.com/'
$webApp.IisSettings['Default'].AllowAnonymous=$true
$webApp.update()

PowerShell to Set anonymous access on specific SharePoint sites:

$web = Get-SPWeb https://sharepoint.crescent.com/sites/operations
#Enabled -  lists and libraries; On - Entire web site ; Disabled - Self explanatory 🙂
$web.AnonymousState = [Microsoft.SharePoint.SPWeb+WebAnonymousState]::Enabled 
$web.Update()

Enable anonymous access on all sites under a web application:

(Get-SPWebApplication https://sharepoint.crescent.com | Get-SPSite | Get-SPWeb | Where {$_ -ne $null -and $_.HasUniqueRoleAssignments -eq $true } ) | ForEach-Object { $_.AnonymousState = [Microsoft.SharePoint.SPWeb+WebAnonymousState]::On; $_.Update(); }

Enable Disable anonymous access at List or library level in SharePoint 2013 / 2016:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Get the web and list
$web = Get-SPWeb https://sharepoint.crescent.com/sites/operations
$list = $web.lists.tryGetList("Documents")

if($list -ne $null)
{
    $list.BreakRoleInheritance($true)
    $list.AllowEveryoneViewItems = $true
    $list.AnonymousPermMask64 ="Open, OpenItems, ViewListItems, ViewVersions, ViewFormPages, ViewPages, UseClientIntegration"
    $list.update();
}

To disable anonymous access, You can either set the Permission mask as: $list.AnonymousPermMask64 =”EmptyMask” or reset inheritance by calling: $list.ResetRoleInheritance()

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

2 thoughts on “Enable Anonymous Access in SharePoint using PowerShell

  • How do you setup an External content type list to use the secure store application via anonymous? I’ve run the scripts above, but it’s not working. I’ve also tried exporting the BDC xml file and manually added the “NT AUTHORITY\ANONYMOUS LOGON” user. None of it seems to be working. We are running Sharepoint 2019 on-Prem.

    Reply
  • Great piece!

    I am several anonymous links that when you click on any of the links, it says they expired. We are getting this message because we did not know that by default, anonymous links expire in 30 days. Is there a powershell script that reverses these expired links and make them active?
    Thanks in advance

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *