I wanted to see which site collections in our 2010 environment are not following the rules and are deactivating the Master Page in the settings.

I have some powershell code that works just fine, and it returns back the correct information but I get a bunch of exception errors. Anybody have any suggestions on how what I might be doing wrong?

Error message:

Exception calling "GetFile" with "1" argument(s): "Value does not fall within the expected range." At line:13 char:35 + $MasterPage = $Web.GetFile <<<< ($Web.MasterUrl) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

Here is my powershell script that I am using:

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

    #Get all site collections of provided web app

$SiteCollections = Get-SPWebApplication | Get-SPSite -Limit All

   #Loop through all site collections
   foreach($Site in $SiteCollections)
       #Loop through all Sub Sites
       foreach($Web in $Site.AllWebs)
        #Get the Master Page
        $MasterPage = $Web.GetFile($Web.MasterUrl)
        if ($MasterPage.name -ne "corp.master")
                     $MasterPage.Name  + " : " + $Web.Url 


Thanks guys!

1 Answer 1


Why not just check the CustomMasterUrl and MasterUrl properties that are on the SPWeb object itself and bypass the slow and expensive GetFile() altogether?

        if ($Web.MasterUrl.EndsWith("corp.master") -eq $false)
           $Web.MasterUrl + " : " + $Web.Url     
  • Could you elaborate on that a little? I originally had used Get-SPWebApplication | Get-SPSite -Limit All | Get-SPWeb -Limit All | Select URL, MasterUrl. But I wasnt sure how to filter out what I needed. I wanted it to return only the site collections that were using something other than "corp.master".
    – Kevin
    Apr 4, 2013 at 19:01
  • updated answer to include an example
    – Dave Wise
    Apr 4, 2013 at 19:26
  • Thanks, Dave! I ended up using that bit of code and modifying it a little to output to a text file.
    – Kevin
    Apr 9, 2013 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.