Learning Center
Plans & pricing Sign in
Sign Out

Intro to PowerShell


  • pg 1
									Intro to PowerShell

                   Adam Preston
                Senior Consultant
               December 4th, 2010
                Who is this guy?

Adam Preston – Senior Consultant (TCSC)
Born in Richmond, VA
–   MCITP: SharePoint 2010, Administrator
–   MCTS: SharePoint 2010, Configuration
–   MCTS: Microsoft Office SharePoint Server 2007, Configuration
–   MCTS: Microsoft Windows SharePoint Services 3.0,
Contact Information:
– Email:
– Twitter: @_apreston
– Blog:
The Computer Solution Company (TCSC)

Founded in 1981
Based in Richmond, VA
Consulting Services and Software Product Development
Consulting Services has been specializing in SharePoint
services for over 6 years (since v1)
Consistently recognized for quality of technical services
– Microsoft Worldwide Partner Conference Technology Innovation
  Award Finalist
– Microsoft Mid-Atlantic Teamwork Award
– Microsoft Mid-Atlantic Loyalty Award
– Infineon Technologies’ Technology Innovation Award
Providing Microsoft-centric consulting, application
development and work management solutions

What is PowerShell?
Where do I find it?
What are the benefits of PowerShell?
Wait, what happened to STSADM?
What commands will get me started?
Who has permission to SharePoint 2010 w/PowerShell?
Demos, Demos, and Demos!

Windows PowerShell is
Microsoft's task automation
framework, consisting of a
command-line shell and
associated scripting language built
on top of, and integrated with the
.NET Framework.
PowerShell provides full access to
COM and WMI, enabling
administrators to perform
administrative tasks on both local
and remote Windows systems.
DOS on Steroids!
A modern replacement for the
DOS command-line shell

PowerShell 2.0 is built into
Windows 7 and Windows Server
2008 R2
For older OS’s, need to download
Windows Management
Framework to get PS 2.0
Start > Accessories > Windows
SharePoint 2010 Management
 –   Simply a shortcut to a profile that loads the
     SP2010 Provider Snapin

Still there and still works
 – Remains for backwards compatibility
 – Allows for portability of existing STSADM batch files and scripts
 – Some commands have been removed (ex. all SSP related commands)
Don’t use this as a crutch! You should start to do everything in PowerShell.
Remember that it’s the “14 Hive” now
 – C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN
PowerShell > STSADM
 – Newly created cmdlets were developed for enhanced performance
 – PowerShell has full access to the SharePoint API from the command line
STSADM to Windows PowerShell Mapping:
                   PowerShell ISE

Windows PowerShell Integrated
Scripting Environment (ISE)
Host application for PS
Can run commands, write, test,
and debug scripts
Uses color-coding
Only available with PS 2.0
Server Manager > Add Features >
Windows PowerShell ISE
Needs to be installed in order to
use Out-GridView
               Benefits of PowerShell

Interact with .NET objects directly
Remoting interface
 – SharePoint PowerShell Remoting Requirements
 – SharePoint Remoting with PowerShell 2.0
Reduction in the need of some 3rd party solutions
Perform administrative functions in bulk, reducing time and effort.
 – Will show an example of this in a later demo
Some SharePoint 2010 functions require the use of PowerShell:
 – Resetting the Farm Passphrase
      •   How to Reset the Farm Passphrase in SharePoint 2010
 – Configuring the State Service Application
 – Configuring the Publishing Feature’s Super User Accounts
Performance benefits for batch operations

Used to find all the commands
available to PowerShell
Cmdlets generally have a verb-
noun naming convention
 –   Get-ChildItem, Get-Member, Add-
     SPSolution, Backup-SPFarm, Export-SPWeb
Get-Command -noun *infopath*
Get-Command -noun *solution*
List all available SP2010 cmdlets:
 –   Get-Command -PSSnapin

Used to show Synopsis, Syntax,
Description, Parameters, and
Several ways to get help information
on cmdlets:
 –   Get-Help <cmdlet>
       •   Get-Help Get-SPSite -full
       •   Get-Help Get-SPSite -examples
       •   Get-Help Get-SPSite –detailed
       •   Get-Help *-SPSite
 –   <cmdlet> -?
 –   Help <name>
       •   Ex. Help database, Help Get-SPSite, etc.
 –   Some results will only show a subset
     ending with “-- More --”
 –   <ENTER> = show next line or row
 –   <SPACE> = show next page or subset
 –   Can create paging by piping results to
       •   Get-SPSite | more
           Aliases and Get-Member

 –   A way of calling a command using a
     keyword or abbreviation
 –   Most cmdlets have an associated alias to
     allow for shortcutting
       •   Get-Command = gcm
       •   Format-List = fl
       •   Set-Location = cd
       •   Get-ChildItem = dir
 –   Use Get-Alias to get a list of aliases
     available for your current session
 –   Use Set-Alias to create your own!
 –   Gets the properties and methods of objects
       •   Get-SPSite | Get-Member
 –   Useful for when trying to determine what
     property you want to output
Navigating PowerShell

Pipeline, Select, Where, and Sorting

 –   Allows output of one cmdlet to be the input of another cmdlet
 –   Examples
       •   Get-Process | Format-Table Name, CPU
       •   Get-SPWebApplication | Get-SPSite | Get-SPWeb

 –   Allows you to pick available properties instead of grabbing all properties
 –   Get-SPSite | Get-SPWeb | Select URL, WebTemplate, LastItemModifiedDate
 –   Only display results which meet a certain condition
 –   Get-SPLogEvent -StartTime (Get-Date).AddMinutes(-5) | Where-Object {$_.Level -eq “High”}
 –   Sort the result set by one or more property
       •   Get-SPSite | Sort Owner, URL
 –   Sort ascending (default) or descending
       •   Get-SPSite | Select URL, Owner, LastContentModifiedDate | Sort LastContentModifiedDate -Descending
                     Ways to Output

 – Outputs to PowerShell session
 – Outputs to provided path
 – Can use “>” as alias
 – Requires PS ISE
 – Sends to default printer

Get-Command Format-*
– Results are:
     • Property1: Result
     • Property2: Result
     • Property3: Result

– Results are:
     • Property1           Property2   Property3
     • Result              Result      Result

– Only displays one property and puts it into multiple columns
Result Filtering, Output, and Formatting

PowerShell Providers and Scripting

 Most Microsoft products expose their management interface through providers
  –   Windows, SharePoint, Exchange, IIS, SQL, Forefront, etc.
  –   IIS Example:
        •   Import-Module WebAdministration
        •   Set-ItemProperty ‘IIS:\Sites\SharePoint – 80’ -Name -Value ‘C:\Logs\IIS’
  –   Windows Example:
        •   New-ItemProperty HKLM:\System\CurrentControlSet\Control\Lsa\MSV1_0 -Name
            “BackConnectionHostNames” -Value “”,”” -PropertyType multistring

 This means that you could have your script or command interact with multiple
 applications at once.
  –   PowerShell allows for complex scripting, which include functions, loops, parameters, branching, and
      much more!
  –   Scripts are saved using the .PS1 file extension
  –   Restricted by default, can change using Set-ExecutionPolicy
  –   Best Practice is to have your scripts signed, don’t leave your environment in an unrestricted state
SP2010 PowerShell Permissions

Out-of-the-box only the Farm Account has permission to interact with
SharePoint 2010 via PowerShell. This does not include additional Farm
 –   Lists who has rights to run PowerShell commands against the databases
 –   Must be local admin, db_owner, and securityadmin to run this command properly
 –   User is added to WSS_Admin_WPG group on all WFEs and is added to the SharePoint_Shell_Access
     role on the database you specified
 –   Add-SPShellAdmin -username tcsc\dap -database Portal_Content_DB
       •   If the database switch is not used, then the user is added access to the configuration database

 –   Remove-SPShellAdmin -username tcsc\dap
Be Careful!
Permissions and SP2010 Commands

 Bing Visual Search

Search Bing for “PowerShell”, click top tab “Visual Search”

Windows PowerShell for SP2010 Resource Center
Windows PowerShell for SP2010 References
TechNet Script Center Repository
Getting Started with Windows PowerShell for SharePoint Administrators
PowerShell Videos on Channel9
Gary Lapointe’s Custom SharePoint 2010 PowerShell Cmdlets and STSADM
Mastering PowerShell (Free eBook - Over 560 Pages!)
PowerShell Book(s)
 – PowerShell for Microsoft SharePoint 2010 Administrators
 – Windows PowerShell 2.0 Administrator’s Pocket Consultant
    TCSC Office 2010 Giveaway

TCSC is giving away copies of Microsoft Office 2010
Professional Plus
3 Ways To Win (One copy max per person overall)
1. Visit the new TCSC Blog
     Make a comment on the SharePoint Saturday post with the words
      “SharePoint Saturday Rocks” in it
2. Follow @TCSC on Twitter
     Tweet “Rocking out with @TCSC at #SharePoint Saturday Richmond
3. Visit our booth and provide us your business card

To top