ENTERPRISE

SharePoint 2010 : Understanding Windows PowerShell Concepts (part 1)

2/5/2011 5:52:33 PM
Before understanding how to use PowerShell to manage SharePoint, an administrator must first understand how PowerShell functions and must conceptualize the concepts used with PowerShell.

Getting Started with Windows PowerShell

PowerShell is available on all new Microsoft operating systems, and administrators can begin learning PowerShell even when SharePoint 2010 is not installed. To make sure that the latest version of PowerShell is installed, check the Microsoft PowerShell scripting center (http://technet.microsoft.com/en-us/scriptcenter/default.aspx). All the examples in this book were written with PowerShell 2.0.

To start working with PowerShell, go to Start, All Programs, Accessories, Windows PowerShell and run Windows PowerShell. This starts the PowerShell shell that can be used for typing PowerShell commands and executing scripts. The default PowerShell shell is shown in Figure 1.

Figure 1. Viewing the default PowerShell shell on Windows 2008 Server.

On a computer with SharePoint installed, a SharePoint 2010-branded PowerShell shell with preloaded SharePoint cmdlets is installed. To start this shell, go to Start, All Programs, Microsoft SharePoint 2010 Products, SharePoint 2010 Management Shell. The shell also supports both standard command-line commands and the legacy STSADM command-line tool.

Using PowerShell to Display “Hello World!”

A common task performed when learning a new language is how to display “Hello World!”. Creating such an application with PowerShell is simple. Type the following into a shell (you can omit lines starting with #. Everything after # is a comment) and press Enter:

# To comment in PowerShell use a Hash tag followed by your comments.
Write-Host "Hello World!" # you can also put comments after your command

PowerShell scripts can also be saved to a file and executed. Save this Hello World sample to HelloWorld.ps1. Use the standard command-line cd command to navigate to folder in which HelloWorld.ps1 is saved. To execute the script, type the following to the shell:

.\HelloWorld.ps1

Using the Integrated Scripting Environment

PowerShell comes with an integrated scripting environment to ease the creation and testing of scripts and functions. The scripting environment is available in Start, All Programs, Windows PowerShell 2.0, Windows PowerShell ISE. PowerShell ISE is shown in Figure 2.

Figure 2. Viewing the “Hello World” sample from within the PowerShell-integrated scripting environment.

Before running any PowerShell cmdlet, make sure you have loaded the appropriate snap-in. The following code checks if SnapIn has been loaded and loads it if necessary:

if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;


However, if you will be running these samples from the SharePoint 2010 Management Shell, the SharePoint PowerShell snap-in will be preloaded for you, and you can bypass this step.

Other  
  •  Managing and Administering SharePoint 2010 Infrastructure : Using Additional Administration Tools for SharePoint
  •  Managing Exchange Server 2010 : Archiving and compliancy (part 3) - Discovery
  •  Managing Exchange Server 2010 : Archiving and compliancy (part 2) - Messaging Records Management
  •  Managing Exchange Server 2010 : Archiving and compliancy (part 1) - Exchange 2010 Archiving
  •  Managing Exchange Server 2010 : Role Based Access Control (RBAC)
  •  SharePoint 2007 : See What Files or List Items Are Checked Out to You
  •  SharePoint 2007 : Publish a File or List Item
  •  SharePoint 2007 : Use the Datasheet View to Add, Edit, or Delete Items and Files
  •  Exchange Server 2007 : Configure the Client Access Server - Manage Exchange ActiveSync
  •  Exchange Server 2007 : Configure the Client Access Server - Manage OWA
  •  
    Most View
    Samsung Ultra 535 AMD-powered Ultraportable Laptop (Part 1)
    PowerShell for Microsoft SharePoint 2010 : Flow Control and Object Disposal - Flow-Control Cmdlets
    Android 4.0 Ice Cream Sandwich Guided Tour (Part 4)
    Choosing The Right Components (Part 3)
    Sony Xperia Z - A Water-Resistant Full HD Smartphone (Part 2)
    Samsung Ativ Smart PC Review (Part 3)
    DirectX 10 Game Programming : Shaders and Effects - Geometry Shaders
    Inside A PC Game Program
    Plug-in Of the Month - November 2012 : Audacity for Windows 2.0.2 RC4, Firefox 15 Beta 5, Comment Blocker
    Group Test: AMD FX-8350, FX-8320, FX-6300 And FX-4300 - All Vishera Processors (Part 3)
    Top 10
    Wireless Connections: What You Need To Know (Part 5)
    Wireless Connections: What You Need To Know (Part 3)
    Wireless Connections: What You Need To Know (Part 2)
    Wireless Connections: What You Need To Know (Part 1)
    Your Online Habits Could Land You In Prison (Part 3)
    Your Online Habits Could Land You In Prison (Part 2)
    Your Online Habits Could Land You In Prison (Part 1)
    Tecdesk Smart 5500 (Part 2)
    Tecdesk Smart 5500 (Part 1)
    Thunderstruck ASUS Brings Thunderbolt 2 To Your PC (Part 3)