Administering SharePoint with Windows PowerShell

About PowerShell:
In general, there is no requirement to use PowerShell to administer SharePoint. The vast majority of standard configuration options are exposed in the Central Administration UI. However, more advanced features, such as partitioning service applications for multi-tenancy, creating SharePoint databases without GUIDs, as well as automating common repetitive tasks, is where PowerShell begins to really shine. Plus, once you start using PowerShell you’ll wonder how you ever got along without it.
For example, creating multiple collections through Central Administration can be a time-consuming process, one at a time. However, you might find yourself in a situation where site collections in your development environment need to be reproduced in your production environment. With a little knowledge of PowerShell and the SharePoint cmdlets, you can generate a comma-separated list (.csv fi le) of all the site collections and supporting information in the development environment using the Get-SPSite and the Export-CSV cmdlets, and then use PowerShell’s Import-CSV cmdlet with the New-SPSite cmdlet in the production environment to recreate them with a single push of the Enter key. This same process using Central Admin would potentially take a tremendous amount of time and likely result in you spraining your mousing finger. No one likes that.

Microsoft SharePoint 2013 Management Shell
The Microsoft SharePoint 2013 Management Shell, shown in Figure 7-1, is the out-of-the-box SharePoint command-line interface. True, it’s not much to look at. As a matter of fact, it looks a lot like the standard cmd.exe command-line interface. However, it’s a cleverly disguised PowerShell console, with the SharePoint commands registered and ready for use. Open the SharePoint 2013 Management Shell and PowerShell at the same time. If you squint, you might see that they look pretty similar. The only obvious difference is that the SharePoint Management Shell has a black background, whereas the standard PowerShell background is blue. They may look similar, but only the Management Shell will run the SharePoint commands without further configuration.
Using Other Windows PowerShell Hosts
The Management Shell is the only host that is pre-configured to run SharePoint commands, but it is not the only host that an administrator can use. Two common hosts are available to administrators with Windows PowerShell version 3: the standard Windows PowerShell console and the Windows PowerShell Integrated Scripting Environment (ISE), shown in Figure below. Administrators can also use any of the third-party PowerShell hosts available free or for purchase. 


We will be adding commands to the PowerShell ISE host, which provides a multiline editor with breakpoints and other help for scripting. Adding commands to other third-party hosts is similar, though you might need to consult their specifi c documentation if you run into any issues.

The ISE is installed with Windows Server 2008 R2 and Windows Server 2012 but it is not activated by default. To activate the ISE, use the Add Feature dialog of the Server Manager. Below Figure shows the Server Manager’s Add Feature dialog with the ISE feature checked.

To register the SharePoint PowerShell commands with ISE, you must be working locally on a SharePoint 2013 server. The SharePoint 2013 PowerShell commands do not work from remote workstations.
SharePoint 2013 commands must be run on a SharePoint 2013 server. The commands cannot be run from a client. PowerShell 3 does provide a remoting capability whereby commands can be executed from a client to run on the server.

If you fi nd yourself in the standard PowerShell console, or one of the many PowerShell hosts available, and need SharePoint cmdlet functionality, you can use the Add-PSSnapin PowerShell
command to register the SharePoint PowerShell cmdlets: 
Add-PSSnapIn Microsoft.SharePoint.PowerShell
Add-PSSnapIn is a PowerShell command that registers add-on modules with the PowerShell console. The Microsoft.SharePoint.PowerShell snap-in contains the registration information for the SharePoint cmdlets.

This will get you started. To verify that the SharePoint 2013 commands are available in ISE, simply run a SharePoint command such as Get-SPSite. ISE should respond with a list of site collections in the farm. You are required to add the Microsoft.SharePoint.PowerShell snap-in every time you start the host. To avoid this, you can run the command to add the snap-in to your profile, which will run each time you start a host.

You can locate your profile by typing $Profile into the command line, which will return the path to your profile. $Profile is a variable containing the current user’s profi le location. The profile location may be different for each host. The ISE profile is different from the PowerShell profile, so commands for the ISE user’s profile will not interfere with other PowerShell hosts. If the profile exists already, you can use the following command to open it:

Notepad $Profile


will discuss the commands in next post...

No comments:

Post a Comment