IT tutorials
 
Technology
 

Sharepoint 2013 : Working with PowerShell (part 1) - PowerShell Basics

10/5/2013 7:57:51 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

1. What About STSADM?

The web user interface to SharePoint is powerful, but as good as Central Administration is you cannot escape the need to script commands in batch via a command line. SharePoint 2007 included a nifty command-line application, called STSADM. STSADM is a versatile and very powerful tool for administering SharePoint and operates, like many command-line tools, with string parameters and switches.

As SharePoint evolved, from the early beta version of MOSS 2007/WSS 3.0, through SharePoint 2010, and now SharePoint 2013, STSADM also evolved. The problem was that STSADM became bloated with many operations, and yet the tool remains limited by command-line string parameters. Microsoft released PowerShell so we could administer Windows, so it made sense to add support for PowerShell in SharePoint. STSADM still exists as a tool, and some SharePoint administrators will continue to use it until the tool is snatched from their tight clutches. However, Microsoft has deemed PowerShell the new standard for command-line interaction with SharePoint, and we can expect STSADM to depreciate over time.

Many of the operations you can perform in STSADM, you can also perform in PowerShell, but with better control, since PowerShell works with objects instead of string parameters. SharePoint 2013 includes the same operations via STSADM as it always has, but any new functionality is now part of PowerShell. Thus, STSADM commands are a subset of those available in PowerShell.

2. PowerShell Basics

You begin by opening a PowerShell window.

  1. Log in to your SharePoint Application server (the same server hosting SharePoint Central Administration).
  2. Search for the application named SharePoint 2013 Management Shell and execute it.
  3. You should see a new shell window, like that in Figure 1.

    9781430249412_Fig03-01.jpg

    Figure 1. SharePoint 2013 Management Shell

That was easy! Opening a PowerShell window is as easy as opening a command prompt window. If you are familiar with the PowerShell window available via Windows, you might be tempted to use this entry into PowerShell, rather than the SharePoint Management Shell. If you take this route, I shall make you aware of the required plug-in to enable SharePoint Cmdlets.

Loading the SharePoint Cmdlets

Microsoft has provided a number of SharePoint-specific Cmdlets, so you can easily manipulate SharePoint objects (applications, site collections, sites, lists, etc.) via PowerShell. These Cmdlets are available to you via a DLL plug-in, which you must enable in a regular Windows PowerShell window. If you launch the SharePoint Management Shell, the shell preloads the SharePoint Cmdlets for you. Type the following commands into your PowerShell window to see if you have the SharePoint Cmdlets loaded:

Get-PSSnapin -Registered | Where-Object { $_.Name -eq "Microsoft.SharePoint.PowerShell" }

The preceding command executes a PowerShell Cmdlet to get all registered snap-in objects, which you then pipe to another built-in Cmdlet, called Where-Object, to filter the list to a specific snap-in for SharePoint. If the SharePoint snap-in is loaded, you should see details about the registered snap-in object. If the preceding command produces no output, you can register the snap-in with the following:

Add-PSSnapin "Microsoft.SharePoint.PowerShell"

After adding the snap-in, re-run the script to check for the registered snap-in, and you should see details of the snap-in.

Note  It is a good idea to use the SharePoint Management Shell for all SharePoint scripts, since all Cmdlets and functions run within the same thread. Scripts that run in the standard Windows PowerShell use separate threads for Cmdlets and functions, which can cause memory leaks when using SharePoint objects.

PowerShell Syntax

Once you start using PowerShell frequently, you will understand the terminology and syntax for PowerShell Cmdlets and script notation. PowerShell Cmdlets assume the syntax verb-prefix noun. The verb always indicates the action on the Cmdlet, and PowerShell supports the verb standards, listed in Table 1.

Table 1. PowerShell Verbs

Verb Action
Add Adds an object to a container or attaches an item to another item
Clear Removes all objects from a container
Close Changes the state of an object to closed
Copy Copies an object from one container to another
Enter Specifies an action that allows the user to enter a resource
Exit Specifies an action that allows the user to exit out of a resource
Find Looks for an object in a container
Format Formats an object to a specific form or layout
Get Retrieves a specific object; paired with the Set verb
Hide Makes an object undetectable; paired with the Show verb
Join Combines objects to one single object
Lock Secures an object; paired with the Unlock verb
Move Moves an object from one container to another
New Creates a new object instance; the Set verb may often be used
Open Opens an existing object
Pop Removes an object from the top of a stack container
Push Pushes an object to the top of a stack container
Redo Resets an object to a state that was undone; pairs with the Undo verb
Remove Removes an object from a container
Rename Renames the name of an object
Reset Sets an object to its default state
Search Creates a reference to an object in a container; do not use Find or Locate verbs
Select Locates an object in a container
Set Creates an object that contains data, or replaces data on an object; paired with the Get verb
Show Makes an object detectable; paired with the Hide verb
Skip Bypasses an object in a sequence
Split Separates parts of an object, such as a string
Step Moves to the next object in a sequence
Switch Specifies an action that alternates between two objects, such as to change between two locations, responsibilities, or states
Undo Set an object to its previous state; paired with the Redo verb
Unlock Releases a locked object; paired with the Lock verb
Watch Continually monitors an object for changes

The prefix in a Cmdlet is usually a two-letter qualifier that identifies the technology; for example, PS identifies PowerShell, AD identifies Active Directory, and SP identifies SharePoint. Finally, the noun is the affected object or resource; for example, Get-SPSite calls the SharePoint Cmdlet to “get” a new SharePoint “site” object.

Exploring SharePoint Cmdlets

As I mentioned in the earlier sections, SharePoint provides a number of Cmdlets, enabling you to interface with SharePoint and configure your farm. What do you do if you want to see a list of these Cmdlets? In using STSADM, you could provide the –o parameter to see a list of operations. Fortunately, PowerShell has a nice built-in Cmdlet called Get-Command.

Executing the Get-Command Cmdlet with no parameters will list every single available Cmdlet in PowerShell—yes, all of them! Passing the –Noun parameter to the Get-Command Cmdlet allows you to filter the list by a given noun name. If you cast your memory back to the previous section, you may remember that I mentioned standard syntax includes a noun prefix to identify the technology. SharePoint prefixes all nouns with “SP,” so you can easily get a list of all SharePoint Cmdlets as follows:

Get-Command –Noun SP*

The previous command uses a wildcard character, which tells the Get-Command Cmdlet to return any Cmdlets that start with “SP” and end with any other string of characters. There is still a large number of Cmdlets listed just for SharePoint operations; the following command tells us exactly how many (my install lists over 770):

(Get-Command –Noun SP*).Count

I think you need to narrow down the list; the Get-Command Cmdlet allows you to search for specific words in the noun. For example, if you wanted to list all the Cmdlets that deal with subsites (SPWeb) objects, you would use the following syntax:

Get-Command –Noun SPWeb

Figure 2 shows a much smaller (and manageable) list of Cmdlets. You can use the same trick to find all Cmdlets for site collection control—using the SPSite noun, rather than SPWeb.

9781430249412_Fig03-02.jpg

Figure 2. SPWeb Cmdlets

Getting Help

Getting a list of PowerShell Cmdlets is good, but knowing more about a specific Cmdlet is better. PowerShell includes another built-in Cmdlet to get help for a specific Cmdlet: Get-Help. The Get-Help Cmdlet provides details about Cmdlets, providers, aliases, functions, and scripts. The following line demonstrates how to execute the Get-Help Cmdlet to get help about the Get-SPWeb Cmdlet:

Get-Help Get-SPWeb

Figure 3 shows a screenshot of the Get-Help Cmdlet used with the Get-SPWeb Cmdlet as a parameter. As you can see, the Get-Help Cmdlet displays the name, a synopsis, syntax, and description of the Cmdlet passed as argument. The syntax information is probably most important because it details the various arguments for the Cmdlet.

9781430249412_Fig03-03.jpg

Figure 3. Using Get-Help Cmdlet

Note  PowerShell incorporates auto-complete with the Tab key. Typing a ‘-‘, followed by Tab will cycle through the various arguments of a Cmdlet.

The Get-Help Cmdlet includes parameters to get information that is more detailed on a Cmdlet. Passing the –Examples parameter will return various examples to demonstrate the use of the Cmdlet. Passing the –Detailed parameter will give more detail beyond the basic information, and passing the –Full parameter will provide the complete help file for a Cmdlet. Try out the Get-Help Cmdlet on a number of well-known Cmdlets as well as some you have not encountered to get a good understanding of available help information.

The –Parameter argument is helpful in conjunction with the Get-Help Cmdlet because it provides details about a given parameter. Typically, when introduced to a new Cmdlet, you would use the Get-Help Cmdlet to get the syntax of available parameters and then provide the –Parameter argument to get details on a specific parameter for a Cmdlet. You can pass a * as the name to the –Parameter argument to get detailed information on all parameters supported by a Cmdlet.

Aliases

An alias is a shortcut for lengthy Cmdlets. For example, typing the lengthy Cmdlet named Get-SPBrowserCustomerExperienceImprovementProgram might get annoying quickly. By creating an alias for the Cmdlet, you can then use the alias in place of the full-length Cmdlet. Of course, I deliberately chose a Cmdlet name that stood out as long, but the likelihood of needing to get the browser customer experience program too often is small. However, this example does demonstrate that some PowerShell Cmdlets can get quite long.

The following example demonstrates how to create an alias, called web, for the Get-SPWeb Cmdlet:

Set-Alias –Name web –Value Get-SPWeb

You can then get the alias value with the following line:

Get-Alias web

To use the alias, simply replace the full name of the Cmdlet with the alias, complete with required parameters, as follows:

web http://sp2013

Note  Aliases exist only in the current PowerShell session. If you close and reopen the PowerShell window, any previous aliases are gone.

Pipelines

Pipelines, in my opinion, really set PowerShell apart from standard command-line operations in Windows. Pipelines allow passing of output information from one PowerShell script or Cmdlet to another. PowerShell deals with objects rather than strings, and this becomes apparent when using pipelines.

The following Cmdlet lists all SPList objects within the root web of a site collection:

(Get-SPSite http://sp2013).RootWeb.Lists

Looking at the preceding command, you see that I use parentheses around the Get-SPSite Cmdlet and then access the RootWeb property and Lists collection. This further illustrates the fact that PowerShell uses objects—the Get-SPSite Cmdlet returns a SPSite object, which has a property called RootWeb. Now, back to our discussion about pipelines.

If you execute the preceding command, you will get back pages of unhelpful information as PowerShell displays every SPList object returned from the Lists collection. By default, PowerShell displays all information about an object if you do not specify the property or method associated with the object.

The following modified example shows the same command piping the output to the ForEach Cmdlet. The ForEach Cmdlet iterates a collection and allows you to control fine-grained operation on each object in the collection.

(Get-SPSite http://sp2013).RootWeb.Lists | ForEach-Object { $_.Title }

The preceding command pipes the results to the ForEach Cmdlet, which iterates the collection and executes the commands in the curly braces. When working with Cmdlets that iterate over collections or filter collections, the special syntax $_ denotes the current object in the iteration. The preceding command outputs the title of each list in the collection.

 
Others
 
- Exchange Server 2010 Quick Start Guide : Installing Exchange Server 2010
- Exchange Server 2010 Quick Start Guide : Configuring Windows Server 2008 R2
- Exchange Server 2010 Quick Start Guide : Server Sizing Quick Reference
- Microsoft Lync Server 2010 : UCMA (part 3) - Walkthrough of the UCMA 3.0 Components - Toolbox Components, Error Condition Components
- Microsoft Lync Server 2010 : UCMA (part 2) - Walkthrough of the UCMA 3.0 Components
- Microsoft Lync Server 2010 : UCMA (part 1) - Server APIs ,Client APIs , PowerShell
- Sharepoint 2010 : Querying SharePoint Data (part 2) - Querying Using LINQ
- Sharepoint 2010 : Querying SharePoint Data (part 1) - Querying Using CAML
- Windows Small Business Server 2011 : Creating a Data Collector Set (part 3) - Scheduling Data Collection
- Windows Small Business Server 2011 : Creating a Data Collector Set (part 2) - Constructing a Data Collector Set Manually
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us