PowerShell Basics for SharePoint 2013
Read PowerShell for SharePoint 2013 How-To or more than 24,000 other books and videos on Safari Books Online. Start a free trial today.
In This Chapter
- What Is a Cmdlet?
- How Can I See the Possible Verbs for a Noun Command?
- What Is a Parameter?
- What Is a Switch Parameter?
- How Can I See the Possible Parameters for a Cmdlet?
- What Does F3 Do?
- What Does F7 Do?
- What Are Console Commands?
- Path Environment Variable
- Running Unsigned Scripts
- Disabling the Confirmation Prompt
- Generating Inline Credentials
- Referencing an Assembly
This chapter explains some PowerShell basics, to help you acquire a general understanding. The sections and scenarios in this chapter help form a foundation for the tasks and solutions within this book.
What Is a Cmdlet?
PowerShell commands are called cmdlets. They are structured by a verb and noun concatenation in the form of verb-noun. So in the cmdlet Get-Help, Get is the verb and Help is the noun.
How Can I See the Possible Verbs for a Noun Command?
If you know the noun but are unsure of the possible verbs available, you can enter the noun with -? to display the available verb-noun combinations. This also displays additional nouns that are similar to the one you provided. For example, SPSite -? displays the available verbs not only for the SPSite noun, but also for SPSiteAdministration, SPSiteSubscription, and other cmdlets available beginning with SPSite.
What Is a Parameter?
Most cmdlets need values to perform the desired actions. These values are provided to the cmdlet using parameters in the following form:
What Is a Switch Parameter?
A switch parameter is a parameter used with a cmdlet that does not take a value (or that is Boolean in nature and can be set to a false setting). The fact that the switch parameter is present “switches on” that option (true setting) when executing the cmdlet.
Most of the time, switch parameters are optional, but in some cases, you need to provide a switch. This depends on the cmdlet being executed.
How Can I See the Possible Parameters for a Cmdlet?
Entering the cmdlet in the console with -? as a parameter displays information about that cmdlet, along with the available parameters. Listing 2.1 shows an example.
Listing 2.1. Getting Information About a Cmdlet
Some cmdlets, such as Get-SPSite, have different variations of parameters. These variations also are displayed within the information provided by the -? parameter, as Figure 2.1 shows.
Figure 2.1. Using -?with a cmdlet provides various syntax information.
What Does F3 Do?
Pressing the F3 function key in the console window displays the last-executed statement on the current prompt line. Using the up and down arrows, you can page through the other previous statements. This is useful for repeating or correcting previous command entries. Use the left and right arrow keys to place the cursor within the command text for corrections.
What Does F7 Do?
This is probably the best-kept secret in command consoles. Every time I show this to someone, they say they had no idea it was available. If you have been entering several commands in a console screen, pressing the F7 function key displays a menu of the previously executed commands, as Figure 2.2 shows.
Figure 2.2. Pressing the F7 function key presents a command history menu.
Use the arrow keys to change the selection in the menu. Pressing Enter executes the command selected. Pressing F9 on the menu enables you to enter the command number you want to re-execute but only places it on the line (the command is not executed automatically).
What Are Console Commands?
A handful of what I like to call console commands stem from the good ol’ DOS days. They have been available in command prompts since then, with the current PowerShell consoles included. These commands provide standard directory navigation, file handling, and screen handling.
Several common console commands follow:
- cd: Changes the directory
- cls: Clears the console screen
- dir: Displays a listing of the current directory (folder)
- type: Used with a text-based filename and displays the contents of a text file
Path Environment Variable
Solution: Add the local directory path to the PSModulePath variable.
You add the local folder’s path to the PSModule environment variable by entering the following command in the PowerShell command prompt (substituting <folder path> with the local directory path), as shown in Listing 2.2.
Listing 2.2. Adding a Folder Path to the PSModulePath
$env:PSModulePath = $env:PSModulePath + ";c:\<folder path>"
Running Unsigned Scripts
Solution: Use the Set-ExecutionPolicy command.
To run unsigned scripts, you must change the execution policy:
You are prompted with a confirmation. Enter Y and press Enter (or just press Enter—Y is the default).
In some cases, you must set the policy to Unrestricted:
Disabling the Confirmation Prompt
Solution: Include the -Confirm parameter with a $false setting.
When running cmdlets discussed in this book, you might receive a confirmation message to confirm the action being processed. If you use these cmdlets in scripts, the script will not run straight through without prompting for the confirmation.
The cmdlets that have a -Confirm optional parameter can be called in unattended mode by passing in -Confirm:$false to the cmdlet, as shown in Listing 2.3.
Listing 2.3. Suppressing the Confirmation Prompt Example
Remove-item *.png -Confirm:$false
Generating Inline Credentials
Solution: Create a new PSCredential object.
Various cmdlets discussed in this book require SharePoint and/or SQL Server account credentials for proper authentication when performing the desired operations. The examples include using (Get-Credential) as the parameter value, which prompts the user for credentials.
When running these cmdlets in scripts, the prompting for credentials pauses the execution. If you need the script to run straight through, instead of using Get-Credential, you can generate a new PSCredential object inline, as shown in Listing 2.4.
Listing 2.4. Generating Inline Credentials Example
(New-Object System.Management.Automation.PSCredential "domain\user", (ConvertTo-SecureString "password" -AsPlainText -Force))
Therefore, simply substitute (Get-Credential) for the text shown in Listing 2.4 with the proper username and password when using a cmdlet with authentication requirements.
Referencing an Assembly
Solution: Use a reflection declaration, and then use a variable to instantiate an object.
You can reference an assembly in your PowerShell script and then use any objects available by assigning them to a variable. Then you can use any methods or properties within your script. No intellisense exists, so you need to know the object model beforehand. Listing 2.5 demonstrates an example assembly reference and usage.
Listing 2.5. Referencing and Using a SharePoint Assembly
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft. SharePoint") [Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint. Administration") $spFarm = [Microsoft.SharePoint.Administration.SPFarm]::Local $spFarmSolutions = $spFarm.Solutions