Share via


Exercise 3: Working with PowerShell Scripts

In this exercise you will gain exposure to working with PowerShell and the PowerShell add-in created for SharePoint 2010.

  1. Launch the PowerShell console from Windows Start menu. The quickest way to find the PowerShell application is to click in the Search text-box in the Start menu (i.e. the one displaying “Search programs and files” and type Windows PowerShell. Select the Windows PowerShell shortcut, not the “ISE” shortcut.
  2. Once you have a command prompt in the PowerShell console windows, type the following command using the Set-Location cmdlet and single parameter with a path on the local c:\ drive and press [Enter]. This is the equivalent of the well-known DOS CD command.

    PowerShell

    Set-Location c:\Student\Labs\01_Roadmap\Powershell\
  3. The current folder of the PowerShell console should now be located in a folder that has several PowerShell scripts (*.ps1 files) that will be used in this lab. Type the following command which uses the Get-ChildItem cmdlet and passes no parameters and press [Enter] to see a listing of all the .ps1 files in this folder.

    PowerShell

    Get-ChildItem
  4. Type in the following command to open Notepad and the script named Hello.ps1. Press [Enter] after typing the command to execute it.

    PowerShell

    Notepad Hello.ps1
  5. After examining the PowerShell code inside Hello.ps1, attempt to run it by typing .\Hello.ps1 and pressing [Enter]. If the PowerShell scripting support on your VM still has the default execution policy of restricted, the script will not run at all. If the execution policy has been changed to unrestricted, the console will prompt you whether to run the script or not. When writing and testing PowerShell scripts, it is easiest to change the execution policy to Bypass so that scripts can freely run without any user prompts. Type the following command and press [Enter] to enable scripting support.

    PowerShell

    Set-ExecutionPolicy ByPass
  6. Now, try to run the script named Hello.ps1 a second time by attempt to run it by typing .\Hello.ps1 and pressing [Enter]. You should now see the script is able to run and output a simple message to the PowerShell console. Leave the PowerShell console open for later use.
  7. Now it's time to move to a better script editor. Open the Windows PowerShell ISE from the Windows Start menu (ISE stands for Integrated Scripting Environment). As before, The quickest way to find the PowerShell ISE application is to click in the Search text-box in the Start menu (i.e. the one displaying “Search programs and files” and type Windows PowerShell ISE. Select the Windows PowerShellISE shortcut.

    Figure 1

    Start Windows PowerShell ISE

  8. Open Hello.ps1 (located at c:\Student\Labs\01_Roadmap\Powershell\ )in the Windows PowerShell ISE. Drop down the Debug menu and you should see it gives you the ability to run and debug the code inside a PowerShell script. Execute Hello.ps1 by pressing [F5] and examine the output.
  9. Make a small change to add a text message inside Hello.ps1 that is assigned to a variable named $HelloMessage as shown below, and Save your work.

    PowerShell

    $HelloMessage = "Sample message" Write-Host "-----------------------------------" Write-Host "Hello World of Powershell Scripting" Write-Host "Host name: "$(Get-Item env:\computerName).value Write-Host $HelloMessage Write-Host "-----------------------------------"
  10. Run Hello.ps1 one more time in the PowerShell console. You should see your changes and also observe how easy it is to edit and run PowerShell scripts.
  11. Now, it's time to practice debugging and single stepping through a PowerShell script. In the PowerShell ISE application, set a breakpoint by right-clicking a line and selecting Toggle Breakpoint or by pressing [F9] in the first line in Hello.ps1. Now, press [F5] to begin execution. The execution should stop and the breakpoint you set. Now, click [F11] repeatedly to single step through the remaining lines of code inside the script.
  12. In the Powershell ISE application, open the script named LoadSharePointSnapin.ps1. Examine the PowerShell script code inside. There is a call to the Add-PSSnapin cmdlet that loads the snap-in for SharePoint 2010 named Microsoft.SharePoint.Powershell.
  13. Now, leave the Windows PowerShell ISE and return the PowerShell console window. Run script by typing .\LoadSharePointSnapin.ps1 and pressing [Enter]. At this point, you can call the cmdlets provided by the Microsoft.SharePoint.PowerShell snap-in.
  14. Run the Get-Command cmdlet with the following parameter and press return to see a listing of all the cmdlet included with the Microsoft.SharePoint.PowerShell snap-in.

    PowerShell

    Get-Command -PSSnapin Microsoft.SharePoint.PowerShell
  15. In the last step, there are too many cmdlets in Microsoft.SharePoint.PowerShell to be able to see them all at once in the console windows. Try running the Get-Command cmdlet again but this time requesting only the cmdlets based on the cmdlet verb of Get.(Note: to repeat a previous command press the up arrow key in the PowerShell window)

    PowerShell

    Get-Command -PSSnapin Microsoft.SharePoint.PowerShell -Verb Get
  16. Run the Get-Command again with the Verb parameter and pipe the output to a new text file named SP2010Cmdlets.txt.

    PowerShell

    Get-Command -PSSnapin Microsoft.SharePoint.PowerShell | out-file –filepath “c:\SP2010Cmdlets.txt”
  17. Open SP2010Cmdlets.txt with Notepad using the following command to inspect the cmdlets provided by the Microsoft.SharePoint.PowerShell snap-in.

    PowerShell

    Notepad c:\SP2010Cmdlets.txt
  18. Now it is time for you to create a new SharePoint 2010 site collection using the New-SPSite cmdlet. Begin by typing the following command and pressing [Enter] to get help information on the New-SPSite cmdlet which will show you a list of parameters and indicate which ones are mandatory and which ones are optional. Note that optional parameters are enclosed in square brackets.

    PowerShell

    Get-Help New-SPSite
  19. Experiment with the New-SPSite cmdlet to create a new site collection using the parameter values shown below. When you have completed this step you should be able to navigate to the top-level site at https://intranet.contoso.com/sites/Lab01C.
  20. Url: https://intranet.contoso.com/sites/Lab01C
  21. OwnerAlias: Contoso\Administrator
  22. Template: STS#1
  23. Name: Lab01C
  24. Open the PowerShell script named CreateContosoSite.ps1 in the Windows PowerShell ISE. Review the PowerShell script code inside. Now return to the PowerShell console and run the following command from the PowerShell console to call this script and create a new site.

    PowerShell

    .\CreateContosoSite.ps1 Lab01D
  25. When you have successfully run the script in the previous step, you should see an output message like the one shown below.

    PowerShell

    New Contoso site successfully created ------------------------------------- Title: Contoso Site: Lab01D URL: https://intranet.contoso.com/sites/Lab01D ID: [GUID value of the new SPSite created]
  26. Navigate to the new site at the URL of https://intranet.contoso.com/sites/Lab01D and make sure the top-level site behaves as you would expect.
  27. Close both PowerShell windows.
Note:
In this exercise you got some hands-on experience in working with some of the provided SharePoint 2010 PowerShell cmdlets as well as working with custom PowerShell scripts.