First of all sorry if this is an obvious question, I searched a lot but can't find the solution.

When I open an spsite in powershell like this:

 $spsite = Get-SPSite "https://adress"

And then I try to get all the webs like so


I get the following error:

$spsite.allwebs : Exception has been thrown by the target of an invocation
+ CategoryInfo          : NotSpecified: (:) [], TargetInvocationExcept
+ FullyQualifiedErrorId : System.Reflection.TargetInvocationException

I don't understand what I am doing wrong, on my other SP server this works fine.

Thanks in advance,

Edit To be more clear about my problem I will post 2 screenshots Here you can see that I am logged in as sp_admin and I am primary admin in the site collection.

enter image description here

Here you see the commands I execute and the error I get.

enter image description here

Edit 2 $spsite.gettype output:

PS C:\Users\sp_admin> $spsite.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    SPSite                                   System.Object
  • I'm assuming the first powershell line should be $spsite = Get-SPSite "https://address", right? – user298 Aug 3 '11 at 12:31
  • @user298 yes sorry i will edit that ;) – nldev Aug 3 '11 at 12:50
  • Please post full stack trace – Anders Rask Aug 3 '11 at 16:26
  • What does $spsite.gettype() outputs? – Janis Veinbergs Aug 5 '11 at 12:24
  • @Janis Veinbergs Added the output to the main question. – nldev Aug 8 '11 at 6:51

I think this is a straightforward permissions problem. Get-SPSite will allow you to retrieve a SPSite object (...at least a partial one) without having permissions to that site collection. But when you try to access the AllWebs collection it throws an error.

To validate this I create a new site collection and made someone other than me the primary and secondary site collection admins. I then ran Get-SPSite against that site collection and it return the SPSite object, but when I access the AllWebs collection it threw exactly the same error as you see above.

  • 1
    Okay that is strange, when i change myself to be the primary site collection administrator i still get the same error. I am logged in under the correct account. Any ideas? – nldev Aug 4 '11 at 12:10
  • Could be a remoting issue or maybe even the dreaded UAC. Are you running this on a SP server in the farm hosting the site you are running the Get-SPSite against or are you doing this remotely? – Rob D'Oria Aug 4 '11 at 17:35
  • First of all thanks for the help, i uploaded some images in my main post, i am logged in as admin, i am local admin, i am primary site admin, i am running it on the machine that runs the central admin. – nldev Aug 5 '11 at 9:58
  • 1
    It was a permissions problem, i logged in using sp_farm and everything worked fine. I think the difference was in the db_creator and db_securityadmin roles, but do not know for sure, but for future reference thry that! – nldev Aug 9 '11 at 11:21
  • 4
    To use the shell the user must have special previliges. In SharePoint 2010 you can use the add-shelladmin cmdlt: technet.microsoft.com/en-us/library/ee806878.aspx#section3 I think your reference to the missing db permissions is the right way to go. – Marco Scheel Aug 10 '11 at 14:34

All permissions required to correct this error:

  • Site Collection administrator on the SPSite (whether given through Central Admin or Site Settings)
  • PowerShell scripting admin: add-spshelladmin domain\username
  • PowerShell scripting admin on the content databases: get-spcontentdatabase | add-spshelladmin domain\username

note that the powershell commands must be run by the farm account (or someone else who already has these permissions)

  • Just had the same issue, the third bullet here worked for me. – James Love Jul 10 '14 at 11:24

The syntax is correct. Do you access to all sites in the site collection with the user that is executing the powershell prompt? Might be an access denied thing on one of the sites...

  • I run as the farm admin, is that the right way? – nldev Aug 3 '11 at 12:51
  • Yeah, sounds good. Sorry, so I have no idea... – Bernd Rickenberg Aug 3 '11 at 13:08
  • x2 The syntax works on my box, displays all the webs. Did you get an incident ID with the error? – Tom Halladay Aug 3 '11 at 13:48
  • Nope, this is the complete error as i got it. Any ideas? – nldev Aug 3 '11 at 13:51

This code worked for me!

$Site = Get-SPSite $siteUrl
Foreach ($web in $Site.AllWebs)
#do something..

See https://stackoverflow.com/questions/13142327/cannot-get-basic-sharepoint-powershell-script-to-run


I found this approach very reliable because the error message can also result from the site being locked from Central Administration. Elevating permissions in this scenario will still yield the error. Here is what worked for me.

  $allWebs = ($site | Select-Object -ExpandProperty AllWebs)
  foreach($web in $allWebs){
    # do something
} catch {
 if($_.Exception.Message -match "Access to this Web site has been blocked."){
    Write-Warning "`nSite Collection Locked: '{0}'`n" -f $site.Url
  } else {
    # something else is needed like script access to db or site collection full control

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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