PowerShell: How to Export Web Application Policies



This Script is used primarily to export web application policies to a file.  It basically takes one argument ($LoggingDirectory), which I have set in my script by default to C:\ExportPolicies.  You will probably want to edit this script to point to a different location, but it work as-is.  The concept is pretty simple.

  1. Load the SharePoint PowerShell Snap-in
  2. Set your script parameters
  3. Check to see if the logging directory exists – if it does, call the ExportPolicies function (I'll explain the function later)
  4. If the logging directory doesn't exist, create the directory and call the ExportPolicies function
  5. Do basic error handling in case for some reason we're unable to create the file (Not to help much, but to return white text instead of red text)

The function is also pretty simple and straightforward.  It accepts the $Filename parameter, which is dynamically generated by appending PolicyReport_%TimeStamp%.txt to the end of the logging directory you specify.  Inside the function, this is what we do.

  1. Get a list of all web applications, assign them to the $WebApplications variable
  2. Create and continue logging information to the text file generated at $Filename
  3. Loop through each of the Web Applications and record the policies for all web applications


That's about all.  The script can be used for documentation purposes, or if you get into a unique situation and need to re-create your web application policies via PowerShell based on their current state.  Any questions or feedback, let me know.  Also, if you feel like optimizing the script and don't mind sharing the details, I'm more than happy to update the script.  This is really just a quick and dirty sample I put together while working on a project.


This script is also downloadable: Download PolicyReport.ps1 (zipped)


if ($ver.Version.Major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}

Add-PsSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

#This PowerShell Script Reports On All Web Application Policies In The Environment And Accepts No Parameters

#Set Script Parameters
$LoggingDirectory = "C:\ExportPolicies"

#Begin Script
######LOAD FUNCTIONS######
Function ExportPolicies ($FileName)
$WebApplications = Get-SPWebApplication


#Record general document header information to the output file

"Policy Reporting Process Beginning at :" | Out-File $Filename
$StartTime | Out-File $Filename -Append
"" | Out-File $Filename -Append
"" | Out-File $Filename -Append
"Current Web Application Policies" | Out-File $Filename -Append


#Record web application specific information to the output file
Foreach($WebApplication in $WebApplications)
#Get an individual web application
$WorkingWebApp = get-spwebapplication $WebApplication

#Get the Web application policies for the individual web application
$WebAppPolicies = $WorkingWebApp.Policies
"" | Out-File $Filename -Append
"Web Application:" | Out-File $Filename -Append

#Record the Web Application Details
$WebApplication | Out-File $Filename -Append

#Record the Web Application Policy Details
$WebAppPolicies | Out-File $Filename -Append

#Record the Start Time of the Policy Report (This is used to generate a dynamic Filename)
$StartTime = (get-date -uFormat "%Y-%m-%d_%I-%M-%S %p" ).tostring()

#Set the Output Filename dynamically
$Filename = "$LoggingDirectory\PolicyReport_" +$StartTime +".txt" 

if(Test-Path c:\temp)
ExportPolicies $FileName
Write-Host " "
Write-Host "Directory $LoggingDirectory Does Not Exist, Creating Directory"
New-Item $LoggingDirectory -type directory
Write-Host " "
Write-Host "Directory $LoggingDirectory Created Successfully"
Write-Host " "
ExportPolicies $FileName
if (Test-Path $FileName)
Write-Host "Policies Exported Successfully To $FileName"
Write-Host "$FileName Could Not Be Created"
Write-Host "Please Try Again"

Comments (0)

Skip to main content