Sharepoint Administration using PowerShell

This guide is for those who already know what Sharepoint is. Basically, the guide’s focus is on Sharepoint Administration utilizing powershell (PoSH)

Variables:

These are simply an object that contains something.  Variables can be identified by a preceding $, such as the “new” variable that follows:

$new

We populate variables with the = sign.  The following instantiates (creates an instance of) a SharePoint web object (called an SPWeb object in the Server Object Model vernacular):

$newSPweb = get-spweb http://url.to/my/subsite

You can name your variables pretty much whatever you like, though there are rules – google them if you are curious, since .Net object naming conventions are slightly out of the scope of this document.

The SharePoint Server Object Model:

This is what makes SharePoint Administration on the command line sooo much easier and fun vs the GUI.  It does have some requirements however – mainly, you must be granted SPShellAdmin privilege over every database you wish to perform operations on.

If you are in a regular PowerShell screen, you can add the SharePoint assembly by typing the following:

Add-pssnapin Microsoft.sharepoint.powershell

If you do not have SPShellAdmin, you will likely receive an error talking about the farm not being available ….

The following is a command that I run with one of our Farm Admin service accounts every time I find my account does not have SPShellAdmin (we have about 20 different farms at work).

Just a note – this command must be run with an account that already has SPShellAdmin – it also must be run with the SharePoint Management Shell app with administrative / elevated privileges:

$spdb = get-spdatabase
$username = “domain\myuser”
Foreach ($_ in $spdb) {add-spshelladmin –database $_.id –username $username}

What this command does is it grabs ALL of the database names that the current SharePoint databases and stores them in that $spdb variable and also says that within the loop, they will individually be addressed using the $_ variable.

The $username variable is the username you wish to get SPShellAdmin privilege to.  The ForEach loop says that, for every item located in the $spdb variable (which is every one of the databases, you can view that simply by typing get-spdatabase), the command between the { } should be ran.  Within the { }, you will see the $_.id – what this is saying is that the particular database item’s ID (GUID) should be used with the –database parameter, and your username used in the –username parameter.

Assuming all went well you will be presented with my favorite output of all absolutely nothing! This counts as one of those ‘no news is good news’ situations.

Moving on ….

Now that we have authorization to be in here, sign out of your service account and sign in with your regular account.

After you are back in, pull open a regular old PoSH window and add the SharePoint assembly:

Add-pssnapin Microsoft.sharepoint.powershell

Next, type

Get-SPWebApplication

to see all the web applications in your farm.
Lets put one in a variable:

$SPWebApp = get-spwebapplication http://urlToMyWebApp
$SPWebApp | get-member

You should see a giant list of stuff on your screen now – these are all of the member objects that are available through the Object Model to you for play with.

$SPWebApp.sites | select rootweb, url

You should now see a list of all the site collections you previously created in your web application.

Play around with get-member and select using the |  …. That vertical line is called a Pipe – it is how you can pass objects from one command to another.

You may also want to try hitting the TAB button on a partially finished command to see what happens (this is called Tab-Completion and is by far my fav part of modern command line interfaces!)

Kevin Arrows


Kevin is a dynamic and self-motivated information technology professional, with a Thorough knowledge of all facets pertaining to network infrastructure design, implementation and administration. Superior record of delivering simultaneous large-scale mission critical projects on time and under budget.

Expert Tip

Sharepoint Administration using PowerShell

If the issue is with your Computer or a Laptop you should try using Reimage Plus which can scan the repositories and replace corrupt and missing files. This works in most cases, where the issue is originated due to a system corruption. You can download Reimage by clicking the Download button below.

Download Now

I'm not interested

Close