Using PowerShell to access SharePoint Sites, Users, and Groups

In this post I am going to dive into some of the basics of accessing your SharePoint sites using PowerShell. This is a pretty basic concept, but something that I think is worth knowing. Understanding the basics allows us to dive deeper into the more complex stuff.

So let’s start! On one of the SharePoint servers, open your SharePoint 2010 Management Shell (Start > All Programs > Microsoft SharePoint 2010 Products). You may have to right click on the app and select Start as Administrator to get the correct permissions.

PowerShell Window

You will get a nice black window, type in cd\ to get to the root. This gives us a little more room to work with. At any time, you can type in cls to clear the screen, this is helpful to keep your eyes focused.

First things first, let’s get a site collection. Type in the following, followed by the Enter key

$site = Get-SPSite http://yoursitename

Now the $site object is your site collection. If it worked correctly, you’ll just be returned to the command prompt

Get-SPSite

Let’s make sure you have the right site, so lets check the URL. Type in the following and press enter after

$site.URL

And that should return your address to the site collection. Want to see all webs in your site? Type in

$site.allwebs

SPSite all webs

Pretty neat eh? You can get the site owner now

$site.owner

SPSite site owner

You can explore the site object and all of its properties and methods by referencing MSDN article http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite_properties.aspx .

Initially we used the Get-SPSite command to get a site. If you leave the address off, and just type in get the command, we can get all sites back

get-spsite

Get-SPSite

Using the previous reference to MSDN, we can add additional properties for a nice report. Let’s pull all site collections with their associated content database.

get-spsite | format-table -property URL,contentdatabase

Get-SPSite properties

And you’ll see all my site collections are in the same database.

Ok, back to our first $site. Now let’s get the root web and check the name.

$root = $site.rootweb
$root.title

Get root web

You just got your first site! Good job! There’s a slightly faster way to get a website. Instead of getting the site then getting the web, you can just call Get-SPWeb http://youraddress, and that will return the web for you.

So if you want to grab a subsite, you would call

$web = Get-SPWeb http://address/site/site/site

Now that you have a web, you can explore it’s properties and find out some more information about it. Check out MSDN article: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb_properties.aspx.

Back to our $root. Let’s get a list of all users in the web.

$root.allusers

Next, let’s see who’s a site admin, and whether or not any of these users are domain groups.

$root.allusers | format-table -property displayname, issiteadmin, isdomaingroup

The additional properties, issiteadmin, isdomaingroup, are from the SPUser object (see http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spuser_properties.aspx for more detail).

Let’s check out the groups of the site.

$root.groups | format-table -property name, owner, users

If you run just $root.groups (try it) it’ll dump out a lot of information at once. Formatting and selecting our data makes it easier to work with.

The above results tells us the groups of the site, and who owns it, and a sneak peak of the users within the group. Let’s explore one group to see all of the users in it.

$group = $root.groups["Sideswipe Members"]
$group.users

To get a user, you need to know the user’s ID or email address. To find this info, you can add the properties to the above command

$group.users | format-table -property displayname, id, email

No, my users do not have email addresses, so I’ll have to use the ID. Either way, you would use

$user = $group.users.getbyemail("emailaddress")

or

$user = $group.users.getbyid(17)

Confirm you have the right users

$user.displayname

This was a very top level overview of accessing some of SharePoint’s objects. From here, now that you know how to get some of the common objects, you can query some more, update values and properties, add users to groups, etc. Microsoft’s MSDN site is extremely helpful in exploring these objects.

Please leave a comment below if you want to see something more specific.

15 thoughts on “Using PowerShell to access SharePoint Sites, Users, and Groups

Add yours

  1. I’d love a point in the right direction for getting more info about authors. Counting the number of edits they’ve made in total (given we’ve got workflow on, and we always approved, I don’t think ‘published by’ will quite do the trick).

    1. You’ll need to either look at the workflow history, and see the comments to determine what changed when or look at the item history (if you have versioning turned on) and look through there for who changed what when. Or both? Email me and we can continue the discussion, david.lozzi at slalom.com. Thanks!

  2. I really appreciate this blog post. Your style of writing is excellent. I find things like this to be great to someone like me who is trying to learn how best to use powershell in the day to day sp2010 administration.

    Have you ever worked on the use of PowerShell and workflows? I occasionally get bounced mail in the farm admin mailbox when a workflow task was assigned to a user who either is no longer at the company or their mailbox is full, etc.
    There is not enough info in the bounce to tell where in the farm the workflow is, so I can’t determine what site admin to contact about the issue.
    I was hoping that powershell might be able to be used to find the URLs of farm active workflows and the potential task assignees defined within those active workflows . That would allow me to determine whether any of the workflows need to be updated .

    Thanks for any ideas.

    1. That is a great idea! I haven’t targeted workflows with PowerShell yet, but it should be pretty straight forward. If you’re familiar with C#/PowerShell, you can probably get some pointers from the source code on my CodePlex project, https://sp2010adminpack.codeplex.com/wikipage?title=Workflow%20Manager&referringTitle=Home. This web part lists all workflows and their associated details. From there, you may be able to dive into the workflow actions, but I’m guessing not. You should be able to at least iterate through the associated tasks and who they’re assigned to.

      I like this idea and will load it up on my to do list, but there’s a lot there, I can’t say when I’d get it done.

      1. Thank you for your reply. I don’t have any familiarity with C#, and my powershell knowledge is pretty rudimentary. Thank you for the pointer – I will have to see whether your codeplex project provides the information I am seeking.

        Thank you again for your kindness.

  3. I’ve been banging around PowerShell sites for a month, looking for answers and hints, and this is the clearest explanation of the most valuable basic information I’ve seen anywhere, including books on the topic. Thanks and Kudos.

  4. I have some groups within groups, How would I get those group members?
    Specificaly I have “Builtin\Administrators” as a user in Farm Administrators.

    1. Getting users out of AD groups will require you to traverse AD. I have some code somewhere, I’ll have to find it and post it. I don’t know however if that’ll traverse Builtin\Administrators, as that’s not an AD group, it’s more of a Windows Server thing. I guess I’ll try and find out. Give me a few days to get the post together. I’ll reply again here.

      Thanks,
      David

  5. Great article, thank you but how would I put a wildcard in here, $group = $root.groups[“WILDCARD Members”]?

Leave a Reply

Up ↑

Discover more from David Lozzi

Subscribe now to keep reading and get access to the full archive.

Continue reading