Your SlideShare is downloading. ×
  • Like
Introduction to windows power shell in sharepoint 2010
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Introduction to windows power shell in sharepoint 2010


2nd SPS Vietnam from Gia Mai

2nd SPS Vietnam from Gia Mai

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide
  • What => Why => Who => Where => When => How
  • What is Shell?Working with a command-line interface, often called a shell, can be a daunting task. If you grew up working with DOS or Unix-based system, you'll feel right at home with PowerShell.If you're used to a computer that always presents you with a Graphical User Interface(GUI), then typing your commands into PowerShell may seem like going backward in time. After you become familiar with a command-line user interface, however, don't be surprised if you get fond of the speed, flexibility, and robustness you find there; you may get a hooked.What is PowerShell?Unix-like shellObject oriented.NETCommand lineScripting languageA single mindset for management of many different systems (e.g.: AD, Exchange, Windows)Extensible framework that can be embedded inside other applications
  • Over the last few years, PowerShell scripting has begun to replace older DOS-style batch files and VBScript files as the preferred way to manage and automate administrative tasksCmdlets are callable functions Pipelining allow one Cmdlet to return an object as input to anotherPowerShell includes formatting features to display output using lists or tablesPowerShell is based on a provider-based model based on Snap-insSharePoint support added through Microsoft.SharePoint.Powershell..Snapin: Group of PowerShell functionality
  • Previous version of WSS relies on STSADM.EXEUsed for command line administration and scriptingPSDrivesFileSystem Provider- access to the file systemMap to key SharePoint directoriesSTSADM via PowerShellExecute operations via the PowerShell hostIntegrate STSADM operations into the pipelineScripting via Object ModelAccess SharePoint objectsCreate reusable SharePoint-aware commandsCustom DevelopmentCmdLetsProvidersHost ApplicationsAutomated build and deploy: => Save time for Deploy/Test Cycle phases
  • This is expensive! The first operation essentially iterates over every site twice—AND it has to start and stop the stsadm logging/process/etc for each feature activation!!The first took 12+ hours and NEVER FINISHED.The latter took roughly 30 minutes.
  • Automation: using Task Scheduler for running Backup and Restore.Development:Testing: usingPowerShell for stress test
  • The first example shows calling a Cmdlet that takes no parametersThe second example adds filtering by adding a Where commandWhere-Object {$ –eq "F*"}The syntax $_ refers to the object in question$ refers to the object's name property-eq is the operator for equalsThe third example adds in formatting instructionsThe last example redirects output so it is stored in a new text files.
  • Get-Help cmdlet
  • You can use aliases to save yourself the time and effort of typing cmdletsthat you use frequently. If you want to see all the aliases that PowerShell has mapped to specific cmdlets, use the Get-Alias cmdlet.
  • By default, PowerShell does not allow script to runAdministrator must change execution policy to enable script executionExecution Policy Settingsrestricted (default) – scripts prohibited from executingunrestricted - scripts can execute. Scripts that are signed can run with user interaction. Scripts that are not signed result in prompting user for permission to execute.bypass (developer mode) – scripts can execute and user interaction is suppressed.
  • With the initial release of PowerShell, administrators and developers usually resorted to writing scripts with NotePad. PowerShell 2 provides a nice development environment for writing PowerShell scripts named the PowerShell Integrated Scripting Environment (ISE). When it comes t writing complex scripts with control of flow logic, it is really nice to be able to debug and single step through your code.
  • Thisexample shows a PowerShell script which uses the New-SPSite cmdlet to create a new site. You should observe that the New-SPSite cmdlet returns a SPSite object which is then used to access the SPWeb object for the top-level site.
  • SharePoint Server 2010 adds many Cmdlets for SharePoint administration through a Snap-in provider named Microsoft.SharePoint.PowerShell. The SharePoint cmdlets cannot be called until the SharePoint snap-in has been loaded.There are two common ways to load the SharePoint snap-inCall the Add-PSSnapin cmdlet and pass a parameter with the snap-in nameLaunch the PowerShell console using SharePoint Management Console. This link points to an XML file that loads the SharePoint snap-in in a declarative fashion.


  • 1. Introduction to Windows PowerShell in SharePoint 2010
    Gia Duy Mai
    Solutions Assistant Lead
    Bamboo Solutions
  • 2. Agenda
    Introduction to PowerShell
    Why PowerShell for SharePoint?
    When use PowerShell?
    PowerShellvs STSADM
    Getting started with PowerShell
    Controlling PowerShell with variables, loops, logic, and functions
    Creating your own PowerShell scripts
    Scheduling your PowerShell scripts
  • 3. Introduction to PowerShell
    • What is Shell?
    Working with a command-line interface, often called a shell
    • What is PowerShell?
    • 4. A modern replacement for the CMD (command) shell
    • 5. PowerShell works with objects
    • 6. PowerShell integrates with the .Net Framework
    • 7. PowerShell is based on a provider-based model
    • 8. A powerful scripting environment for administration
  • PowerShell Fundamentals
    PowerShell code in a PS1 (similar to .bat files)
    A PowerShell command is called CmdLets
    Like Features in SharePoint, performs a task
    Can be compiled or in script
    Ex: Get-Process and Stop-Process
    Like Solutions in SharePoint, they hold a lot CmdLet
    Compiled into dlls
    Ex: Microsoft.SharePoint.Powershell
    Formatting features
    Provider-based model for accessing resources
  • 9. The Pipeline
    What is Pipeline?
    • The most unique feature of PowerShell
    • 10. Pipelining allow one Cmdlet to return an object as input to another
    X = Class.Method();
    Y = OtherClass.Method(X);
    Z = OtherOtherClass.Method(Y)
    Class.Method() | OtherClass.Method() | OtherOtherClass.Method()
  • 11. Why PowerShell for SharePoint 2010?
    Automated build and deploy
    Rapid prototyping
    Remoting interface
    Administration automation
    Easy for controlling, manipulating and modifying our SharePoint environment
    • PowerShell support effectively replaces STSADM
    We’ll miss you old friend
  • 12. PowerShell vs STSADM
  • 13. PowerShell vs STSADM - Performance
    Performance benefits for batch operations
    STSADM Batch Operation
    PowerShell Cmdlets in Batch
    Start Thread
    New STSADM Execution
    Start Thread
    Load DLLs
    Start logging
    Start Logging
    Execute Command
    Execute Command
    Stop logging
    Stop Logging
    Stop Thread
    Close STSADM & Thread
  • 14. PowerShell vs STSADM - Performance
    PowerShell “Wrapped” STSADM to enable a feature on every SPSite
    Same command in SharePoint 2010 PowerShell:
    $rawdata = stsadm.exe -o enumsites -url $WebAppURL
    $sitexml = [XML]$rawdata
    $sitesxml.Sites | foreach-object{ $_.Site } | foreach-object{
      stsadm -o activatefeature -url$_.url -filename $featureFileName
             if( $lastexitcode -ne 0 ){
                Write-Host “Failure:$lastexitcode n" -Fore Red;
                $failure = $true;
    12+ Hours
    (5300 SPSites)
    30 Minutes
    (5300 SPSites)
    Get-SPSite –Limit ALL –WebApplication $WebAppNameorUrl |%{
    Enable-SPFeature$FeatureIdOrName –url $_.Url
  • 15. When use PowerShell?
    When you want to make your team more agile
    - Automation, automation, automation
    • Development phase
    - Using PowerShell to save time for Build/Deploy/Test Cycle
    Testing phase
    Using the PowerShell scripts to stand up an environment for running tests
    Traditional SharePoint Development
    Wasted time for testing
    When you use PowerShell
  • 16. Who should learn PowerShell?
    • SharePoint Admins/Devs should learn PowerShell basics
    Why you should learn PowerShell?
    Microsoft says it’s important
    You can’t do everything from the GUI any more
    It can make your life easier
    Most Microsoft products will eventually use it
    Microsoft certification exams contain PowerShell questions
    Improve your job
    It’s not going away anytime soon
  • 17. Getting Started with PowerShell
    (1) Execute a Cmdlet
    (2) Filter results using where clause
    (3) Add formatting instructions
    (4) Redirect output to new text file
  • 18. Getting Help in PowerShell
  • 19. Controlling PowerShell with variables, loops, logic, and functions
  • 20. Variables in PowerShell
    A variable is a place for you to store some value so you can revisit it later.
    All variables that you create begin with $
    Ex: $my_first_variable= “Hello World”
    Variables Types
    Loosely typed variables
    $f = “bar” #implicitly typed as string
    $ary= 4,2,5,2 #typed as object[]
    Strongly typed variables
    [string]$f = “bar”
    Enhanced Types variables
    [xml]$d = “<a><b><c>c stuff 1</c><c>c stuff 2</c></b></a>”
  • 21. Looping in PowerShell
    Classic PowerShell Loop
    - For Loop
    $i = 10
    For($j = 1; $j-lt $i; $j++){ Write-Host ”$j”; }
    - ForeachLoop
    $NumArray= (1,2,3,4,5,6,7,8,9,10)Foreach($Item in $NumArray) {$Item * 3}
    - While Loop
    $i =8 While ($i-le 96) { $i+=8; Write-Host$i; }
    - Do Until Loop
    $i = 7;
    do { $i+=7; Write-Host$i; } until ($i-gt 85)
    ForeachLoop with Pipeline
    Get-SPSite -Limit All | foreach {
    Remove-SPSite -identity $_.Id }
    $_ and is a variable that represents the current object
  • 22. Adding logic in PowerShell
    Logic is what enables you to do something at a specific point in the program.
    If/ ElseIf/ Else
    Comparison Operators
    -lt / -gt
    -le / -ge
    -like / -notlike

  • 23. Working with PowerShell functions
    { Get-ChildItem–Path $DriveLetter}
    Function ListRootDrive
    { Get-ChildItem–Path $Args}
    { Get-ChildItem–Path $Args[0] }
    FunctionBlogUsers($SiteCollection) {
    Get-SPWeb-Site $SiteCollection| ForEach-Object { Write-Host “Site Title: “ $_.Title
    if ( $_.Title -eq “Blog” ) { Write-Host “Blog Users:” $_.Users
  • 24. PowerShell Aliases
    PowerShell commands are usually long. However, PowerShell has aliases. You can use aliases to save yourself the time and effort of typing cmdlets
    • Get-ChildItem ~ dir
    • 25. Foreach-Object ~ %
    • 26. Get-ChildItem|foreach-object { $_.Name }
    • 27. Alias: dir|% { $_.Name }
    • 28. Where-Object ~ ?
    • 29. Get-ChildItem|where-object{$_.Length –gt10}
    • 30. Alias: dir|?{$_.Length –gt10}
    You can find these aliases with the Get-Aliascmdlet
  • 31. Creating your own PowerShell scripts
  • 32. Tools
    PowerShell Command
    Windows PowerShell Integrated Scripting Environment (ISE)
    - Download from
  • 33. How to Create and Run a PowerShell Script
    The steps to create a script follow:
    Create the script in Notepad and save with a .PS1 file extension (hello.ps1).
    Run the script by entering the full path to the script (c:scriptshello.ps1), or if it’s in the current directory (.hello.ps1).
    If the path to the script contains a space, enclose the full path in quotation marks and prefix the entire thing with an ampersand ("C:my scriptshello.ps1").
  • 34. Scripts and Execution Policy
    Local execution policy must be configured
    Scripts will not execute under default execution policy of restricted
    Scripts can execute under execution policy of unrestricted. Scripts that are not signed result in prompting user for permission to execute.
    Scripts can execute under execution policy of bypass. This mode suppresses prompting user for permission to execute.
  • 35. Editing Scripts using PowerShell ISE
    Supports Color-coding, IntelliSense and debugging
    Writing scripts here
    Run cmdlets or functions here
  • 36. Examples of SharePoint Scripting
  • 37. The SharePoint PowerShell Snapin
    Explicitly load SharePoint PowerShell snap-in from console or script
    Implicitly load SharePoint PowerShell snap-in by using SharePoint 2010 Management Shell
    Begin using SharePoint Cmdlets
  • 38. Scheduling Your Scripts
    Sometimes commands need to be scheduled to run automatically
    PowerShell Scripts (.ps1) can be scheduled using Task Scheduler
    Just put the above command into a .bat or .cmd file and schedule it like you would normally schedule a script to be run with Task Scheduler.
  • 39. Demo – Series Of Scripts
    Run hello.ps1 script
    How to use PowerShell to create sites.
    How to use PowerShell to populate items in a List for testing, adding attachments
    How to use PowerShell to copy items from one list to another.
    How to use PowerShell to upload documents to a document libraries.
    How to use PowerShell to backup sites.
  • 40. Q&A
  • 41. Resources
  • 42. THANK YOU!