Enable Anonymous Access in SharePoint using PowerShell

How to Enable Anonymous Access in SharePoint using PowerShell?
In continuation with my last post, How to Enable Anonymous Access in SharePoint 2013, 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 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 'http://sharepoint.crescent.com/'

PowerShell to Set anonymous access on specific SharePoint sites:

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

Enable anonymous access on all sites under a web application:

(Get-SPWebApplication http://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:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

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

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

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 is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

One thought on “Enable Anonymous Access in SharePoint using PowerShell

  • March 10, 2019 at 12:55 AM

    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


Leave a Reply