
Authoring, debugging, and executing script accessing farm settings with PowerGUI and PowerShell ISE
As you can see from the previous recipe, authoring and executing a PowerShell script is a simple task that can be done right from the command line. In this recipe, we'll take a look at how you can author and debug your PowerShell scripts using two of the most popular tools: PowerShell ISE and PowerGUI. Using these tools, we'll execute a script accessing farm settings of the SharePoint site.
Getting ready
First, let's ensure you have installed PowerShell ISE:
- On the target Virtual Machine, click Start | Administrative Tools | Server Manager.
- On the left-hand side panel of the Server Manager window, click the Features node.
- In the main window of the Server Manager, click Add Features.
- From the Add Features Wizard, ensure Windows PowerShell Integrated Scripting Environment (ISE) is selected. If it is selected and grayed out, as seen in the following screenshot, skip to Step 6 in this sequence.
- Click Next and Install on the following window to install the feature.
- Upon installation completion, close the Server Manager window.
Let's now install PowerGUI:
- Navigate to
- Download the latest version of PowerGUI installer.
- Run the installation package on your development environment and install the PowerGUI tool using the default installation options.
Now that you have all of the tools installed, let's use PowerShell ISE and PowerGUI to author, debug, and execute our new script.
How to do it...
Let's see how PowerShell ISE and PowerGUI can help with your script authoring.
- On your development environment, click Start | All Programs | Accessories | Windows PowerShell | Windows PowerShell ISE.
- In the PowerShell ISE window's top section, type in the following script:
$siteUrl = "http://intranet.contoso.com" $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'} if ($snapin -eq $null) { Write-Host "Loading SharePoint Powershell Snapin" Add-PSSnapin "Microsoft.SharePoint.Powershell" } $site = Get-SPSite | Where-Object {$_.Url -eq $siteUrl} $site.WebApplication.QueryFeatures("00BFEA71-EC85-4903-972D-EBE475780106")
Tip
Downloading the example code
You can download the example code fles for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the fles e-mailed directly to you.
- Press F5 on your keyboard.
- Take a note of the results returned by the script which will contain multiple instances in the following format:
DefinitionId : 00bfea71-ec85-4903-972d-ebe475780106 Parent : My Properties : {} Definition : SPFeatureDefinition Name=FeatureDefinition/00bfea71-ec85-4903-972d-ebe475780106 Version : 3.0.0.0 FeatureDefinitionScope : Farm
- Now let's see the result with PowerGUI. On your development environment, click Start | All Programs | PowerGUI | PowerGUI Script Editor.
- In the top section of the PowerGUI editor, insert the same code we used in step 2 of this sequence:
$siteUrl = "http://intranet.contoso.com" $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'} if ($snapin -eq $null) { Write-Host "Loading SharePoint Powershell Snapin" Add-PSSnapin "Microsoft.SharePoint.Powershell" } $site = Get-SPSite | Where-Object {$_.Url -eq $siteUrl} $site.WebApplication.QueryFeatures("00BFEA71-EC85-4903-972D-EBE475780106")
- Press F5 to execute your script.
- Take a note of the same result set in the PowerShell Console window right below the editor, seen in the previous image.
- Switch back to the script editor section of the screen and set your cursor on the last line of the code.
- Press F9 to set the breakpoint on the last line of the code.
- Press F5 to execute the script up to the breakpoint.
- Take a note of the script editor window when the script has been executed up to the breakpoint. Your PowerGUI editor will look similar to the following screenshot:
- At this point you can press F5 on your keyboard to continue execution.
How it works...
We launched the PowerShell ISE to execute our custom script. The first thing our script is going to do is load the PowerShell cmdlet library for SharePoint. This extension library holds various PowerShell functions allowing us to work with SharePoint objects from within PowerShell. Once the library is loaded, our script connects to our SharePoint site,

This function can be pretty handy when you're trying to locate problem features, or determine which site will be affected by the planned feature upgrade.
Our PowerShell script has been executed first in PowerShell ISE to see what capabilities you have in this Integrated Scripting Environment (ISE).
We then used PowerGUI to see how the same script can be executed and debugged. As you can see, PowerGUI has a few more features facilitating the script authoring process.
The debug option available in the script editor is particularly handy when your script doesn't quite yet work to your standards, and you want to figure out potential problems in it. If you're a developer, you already know all about debugging and its benefits.
Once you're satisfied with the script, you can execute it and run it on the current environment.
There's more
Let's take a look at how we can author and execute scripts with PowerGUI.
Script authoring with PowerGUI
One of the other advantages to PowerGUI is the ability to see values of variables in your script as it executes. The Variables panel is, by default, on the right-hand side of your PowerGUI window as seen here:

Without this panel, if you wanted to list the variable value, you would typically need to call it in a command line. If the variables in question are complex objects, you get to see the value of all the properties too, as shown in the following screenshot:

Also, to aid you with script authoring, PowerGUI has a collection of handy snippets which you can access with the Edit | Insert Snippet option.
Tip
For more tips on working with PowerGUI user interface and features, check out http://www.Powergui.org. For more tips on PowerShell ISE, search TechNet for Windows PowerShell Integrated Scripting Environment.