PowerShell Help
We mentioned earlier that one of the
difficulties in mastering a command-line environment is figuring out
what to type. It can be very frustrating. The designers of PowerShell
were ingenious, though, providing the extremely useful Get-Help command. Whereas the Get-Command cmdlet is useful for determining which commands are available to PowerShell, you can use Get-Help to understand the usage of a command. The Get-Help command is a standard PowerShell cmdlet. To get help for a particular command, simply call Get-Help, passing in the name of the command for which you want help. For example, to view help about New-SPWebApplication, you would use the following:
Get-Help New-SPWebApplication
Figure 3 shows the help returned for New-SPWebApplication. It provides numerous details about the command, including parameters, examples, and usage. Get-Help accepts the optional parameters of –detailed, -examples, and -full. When learning how to use an unfamiliar PowerShell cmdlet, running Get-Help with the –examples
parameter is a great way to quickly get up to speed on how to use it.
Examples of the available options are shown under the Remarks heading
(refer to Figure 3). To learn more about Get-Help, simply use Get-Help Get-Help — a little repetitive, but it works.
Note that the Windows Server product team that
covers PowerShell (a subteam of Server & Tools) at Microsoft has
decreed that every PowerShell cmdlet included with a Microsoft product
must have help information and at least one example. That’s admirable,
but unfortunately not all of the examples are accurate; some of the
SharePoint cmdlets have flakey examples. The good news is that you can
update the help information for cmdlets by running Update-Help.
If your SharePoint server has access to the Internet, PowerShell will
check for updated help for the modules installed. This may fix some of
the erroneous help entries, but be aware that some just don’t work.
PowerShell Variables
It is virtually impossible to ignore
variables, except in the most basic cmdlet usage. The first time you
pipe the output (covered in the section “PowerShell Pipeline”) of one
command into another command you are already using variables. Despite
what some developer “friends” might tell you, variables are not the
second coming of algebra, although the similarities are uncanny. In a
basic sense, variables hold information. All PowerShell variables start
with the $ character, and they can hold any type of valid object.
NOTE Certain objects in SharePoint, such as SPWeb, SPSite, and SPSiteAdministration, must
be properly disposed of. One-liner SharePoint commands will dispose of
all objects correctly. Storing these SharePoint objects in variables
can lead to performance issues and memory leaks if the variable is not
handled correctly.
In Figure 4, the variable $webApps is set to the output of Get-SPWebApplications. $webApps now contains all the web application (SPWebApplication) objects. You use the Count
property to determine how many web applications are contained in the
variable, and you can display the web applications by simply typing the
variable name.
NOTE Wondering
which commands were used to get the output displayed in the screenshots
of the Management Shell? Just take a look at the top of the figures to
see the commands following the administrator prompt.
Note that the Get-SPWebApplication
cmdlet does not include your Central Admin web application by default.
This is helpful because it means you won’t always have to check to
ensure that whatever settings you are applying to your standard web
applications aren’t being inadvertently applied to the Central Admin
site. You have to include the -IncludeCentralAdministration parameter in order for Central Admin to be included.