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.
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
Let’s make sure you have the right site, so lets check the URL. Type in the following and press enter after
And that should return your address to the site collection. Want to see all webs in your site? Type in
Pretty neat eh? You can get the site owner now
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
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
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
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.
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")
$user = $group.users.getbyid(17)
Confirm you have the right users
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.