SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ($ -ne "corp.master")
                     $MasterPage.Name  + " : " + $Web.Url 


Thanks guys!

share|improve this question
up vote 3 down vote accepted

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     
share|improve this answer
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 '13 at 19:01
updated answer to include an example – Dave Wise Apr 4 '13 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 '13 at 13:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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