How to Disable Auditing in SharePoint using PowerShell?

Requirement: Disable auditing in SharePoint site collection.

How to Disable Auditing in SharePoint?
You may want to, To disable auditing for a SharePoint On-premises site collection follow these steps:
  • Go to Site Settings >> Click on "Site Collection Audit settings" under site collection administration.
  • In Configure Audit Settings page, clear all check boxes such as "Opening or downloading documents, viewing items in lists, or viewing item properties", "Editing items", etc. and click on "OK" to save your changes.
    sharepoint disable audit log
    This turns off auditing for the particular site collection. However, when you have a large number of site collections and want to turn off auditing for all, utilize PowerShell!

PowerShell to Disable Auditing for All Site Collections of a SharePoint On-premises web application:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set Web Application URL 
$WebAppURL="http://intranet.crescent.com"

#Get the web application and iterate through each site collection
$WebApp = Get-SPWebApplication $WebAppURL
$SiteColl= $WebApp.Sites

ForEach ($Site in $SiteColl)
{
    $Site.Audit.AuditFlags = [Microsoft.SharePoint.SPAuditMaskType]::None
    $Site.Audit.Update()
    Write-host "Disabled Auditing for Site Collection:"$Site.URL
}

PowerShell to Disable Audit in SharePoint Online:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
  
#Get All Site collections from the Tenant- Including Modern Team sites and communication sites
Function Disable-SPOAudit($AdminSiteURL, $Cred)
{
    #Setup credentials to connect
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
   
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminSiteURL)
    $Ctx.Credentials = $Credentials
  
    #Get the tenant object 
    $Tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)
  
    #Get All Site Collections
    $SiteCollections=$Tenant.GetSitePropertiesFromSharePoint(0,$true)
    $Ctx.Load($SiteCollections)
    $Ctx.ExecuteQuery()
 
    #Iterate through Each site collection
    ForEach($Site in $SiteCollections)
    {
        #Get the Site Collection and Audit Objects
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Site.URL)
        $Ctx.Credentials = $Credentials
 
        $SiteColl = $Ctx.Site
        $Ctx.Load($SiteColl.Audit)
        $Ctx.ExecuteQuery()
 
        #Disable Audit for site collection
        $SiteColl.Audit.AuditFlags = [Microsoft.SharePoint.Client.AuditMaskType]::None
        $SiteColl.Audit.Update()
        $Ctx.ExecuteQuery()
        Write-host "Disabled Audit for Site Collection:"$Site.URL
    }
}
  
#Set Parameters
$AdminSiteUrl = "https://crescent-admin.sharepoint.com/"
$Cred= Get-Credential
 
Disable-SPOAudit -AdminSiteURL $AdminSiteUrl -Cred $Cred 
How to Disable Auditing in SharePoint using PowerShell? How to Disable Auditing in SharePoint using PowerShell? Reviewed by Salaudeen Rajack on December 23, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.