As you will have seen in some of our other blog entries here, we often get asked to assist our customers in creating PowerShell scripts.

I've recently been working on a script to help ensure that a client is able to set site collection auditing. For compliance reasons they needed to be sure that all auditing activities are logged.
The script below enables specific logging options for each site collection:

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

    $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
    $webServices = $farm.Services;
   
    foreach ($webService in $farm.Services)
    {
        if (!($webService -is [Microsoft.SharePoint.Administration.SPWebService]))
        {
            continue;
        }

        foreach ($webApp in $webService.WebApplications | Where-Object {($_.DefaultServerComment -ne 'SharePoint Central Administration v3') -and ($_.DisplayName -eq '<WebApplicationName>') })
        {
            if ($webApp -is [Microsoft.SharePoint.Administration.SPAdministrationWebApplication])
            {
                continue;
            }
           
            $siteCollection = $webApp.Sites
           
            foreach ($site in $siteCollection)
            {
                $site.Audit.AuditFlags = [Microsoft.SharePoint.SPAuditMaskType]::View -bxor [Microsoft.SharePoint.SPAuditMaskType]::Update -bxor [Microsoft.SharePoint.SPAuditMaskType]::CheckIn -bxor [Microsoft.SharePoint.SPAuditMaskType]::CheckOut -bxor [Microsoft.SharePoint.SPAuditMaskType]::Copy -bxor [Microsoft.SharePoint.SPAuditMaskType]::Move -bxor [Microsoft.SharePoint.SPAuditMaskType]::Delete -bxor [Microsoft.SharePoint.SPAuditMaskType]::Undelete -bxor [Microsoft.SharePoint.SPAuditMaskType]::SecurityChange
                $site.Audit.Update();
                $site.dispose();
                 
            }
            $siteCollection.dispose();    
        }           
    }

To use this all you need to do is set the web application name, and change the options to suit your requirements.