Skip to content

PowerShell Processing Every File in SharePoint

I’m doing a migration from SharePoint 2003 to SharePoint 2010 where some files in SharePoint 2003 were “accidentally” customized. So I wanted to process every file and uncustomize it (or ghost it). Rather than writing a command line program as I’ve done dozens of times before, I decided to do it in PowerShell. Even though the specific case I’m doing here is targeted to unghosting, the general framework is pretty handy for processing every file and folder in SharePoint. The script is just pasted below:

<#
.SYNOPSIS
Uncustomizes every file in a farm
.DESCRIPTION
Designed to fix issues because of older versions of customized pages
.PARAMETER None
.EXAMPLE
UnCustomizeAllFiles.ps1
#>
function ProcessFile($file)
{
try
{
if ($file.CustomizedPageStatus -eq [Microsoft.SharePoint.SPCustomizedPageStatus]::Customized)
{
$file.RevertContentStream();
Write-Host -separator ” ” ” Reverting:” $file.Url
}
else
{
# Write-Host -separator ” ” ” Skipping: ” $file.Url
}
}
catch
{
Write-Host -separator ” ” ” Exception” $_.Exception.ToString() “on” $file.Url
}

}

Help Your SharePoint User

function ProcessFolder($folder)
{
Write-Host -separator ” ” ” Folder:” $folder.Url
# process sub-folders
try
{
foreach($subfolder in $folder.SubFolders)
{
ProcessFolder($subfolder);
}
}
catch
{
Write-Host ” Error on processing subfolders”
}

# process files
try
{
foreach($file in $folder.Files)
{
ProcessFile($file);
}
}
catch
{
Write-Host ” Error on processing files”
}
}

$apps = Get-SPWebApplication
foreach($app in $apps)
{
Write-Host -separator ” ” “App:” $app.Name
foreach($site in $app.Sites)
{
Write-Host -separator ” ” ” Site:” $site.Url
foreach($web in $site.AllWebs)
{
Write-Host -separator ” ” ” Web:” $web.Url
foreach($folder in $web.Folders)
{
ProcessFolder($folder);
}
$web.Dispose();
}
$site.Dispose();
}
}

No comment yet, add your voice below!


Add a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this: