Exchange 2013 RTM CU2 includes 965 cmdlets, but you’re not
likely to use the vast majority of these simply because many are
designed for one-time use. For example, after you configure a receive
connector, you probably will not revisit the Set-ReceiveConnector
cmdlet very often after the connector is working. However, you’ll use
cmdlets such as Get-Mailbox daily. Some examples (in no particular
order) of frequently used Exchange cmdlets are the following:
Get-ExchangeServer. Return a list of Exchange servers in the organization.
Disable-Mailbox. Disable a user’s mailbox.
Add-DistributionGroupMember. Add a new member to a distribution group.
Set-Mailbox. Set a property of a user’s mailbox.
Get-MailboxDatabase. Retrieve properties of a mailbox database.
Get-MailboxStatistics. Return statistics about user mailboxes such as the total item count, quota used, and so on.
Note
the consistent syntax of verb (Get, Set, Move, Remove, or Disable) and
noun (Mailbox, User, and so on). Along with commands that operate on
objects, you find commands that help you work with data, such as
Where-Object, Sort-Object, and Group-Object. Where-Object, Sort-Object,
and Group-Object are commonly shortened by using their aliases of
Where, Sort, and Group. You can type Help followed by a cmdlet name at any time to get help on the syntax of the command.
Tip
When
you start to write scripts, consider spelling out cmdlet names
completely and avoiding the use of aliases. This is important because
you can never know in what environment a script will be run and
therefore cannot assume that an alias will be defined and available for
use in your code.
The Exchange developers have
provided very accessible help for the EMS cmdlets. Apart from using the
Help cmdlet, there are other ways of seeking help. RBAC controls limit
help content so that a user sees help only for the set of cmdlets
available to the roles that user holds. You can do the following:
Use
the Get-Command cmdlet to list the cmdlets you can use with different
objects. The set of cmdlets will be limited to whatever is permitted by
the RBAC roles held by your account. For example, Get-Command *contact*
lists all the cmdlets available to work with contacts (shown in the
following example). You can also use the shortened alias of gcm for
Get-Command.
CommandType Name Definition
----------- ---- ----------
Function Disable-MailContact …
Function Enable-MailContact …
Function Get-Contact …
Function Get-MailContact …
Function New-MailContact …
Function Remove-MailContact …
Function Set-Contact …
Function Set-MailContact …
Use the /detailed switch to get more detailed help about a cmdlet. For example: Get-Help Get-CASMailbox –Detailed.
Use
the /full switch to have EMS return every bit of information it knows
about a cmdlet. For example, Get-Help Get-DistributionGroup –Full.
Use
the /examples switch to see whatever examples of a cmdlet in use EMS
help includes. For example, Get-Help Get-MailboxServer –Examples.
Use
the /parameter switch to get information about a selected parameter for
a cmdlet. For example, Get-Help Get-Mailbox –Parameter Server. This
switch supports wildcards, so you can do something like Get-Help
Set-Mailbox –Parameter *Quota*.
Most
of the time, you will probably work with commands by invoking EMS
interactively and then typing whatever individual commands or scripts
are necessary to perform a task. The user interface of EMS is based on
the Win32 console with the addition of features such as customizable
tab completion for commands. After you become accustomed to working
with EMS, things flow smoothly, and work is easy. It is then usually
faster to start EMS and issue the necessary code to change a property
on a mailbox or a server than to start EAC and navigate to the right
place to make the change through the graphical user interface (GUI).
Tip
Working
through EMS is especially valuable if you have to perform management
operations across an extended network link when waiting for the GUI to
display can be painful. If you have a programmatic mind, you can also
call EMS cmdlets through C# code, which is how Microsoft invokes them
in EAC and other places throughout Exchange, such as to set up servers
and databases in the setup program. (The blog Glen Scales writes at http://gsexdev.blogspot.com/
provides many good examples of how to call EMS cmdlets from code.) In
the past, the different groups that contributed to Exchange had to
build their own programming interfaces, whereas now everyone uses
PowerShell.
You can see that EMS focuses on performing
tasks rather than taking the more object-focused approach implemented
in the GUI, something that reflects a desire to accommodate
administrators who think about how to do things rather than how to work
with objects. After all, it is human nature to think in terms of the
task of moving a mailbox to a different server rather than thinking
about how to manipulate the properties of a mailbox object to reflect
its new location.
Cmdlets
accept structured pipelined input from one another in a common manner
to allow them to process data in a consistent manner, no matter which
cmdlet provides the data. Programmers therefore do not have to reformat
data for input to specific cmdlets, so the task of assembling different
cmdlets into a script to do a job is much easier. Microsoft built
PowerShell around the concept of objects, so objects are accepted as
input, and the output is in the form of objects that you can then pipe
to other cmdlets. Even if the output from a cmdlet looks like
plaintext, what you see is one or more objects that you can manipulate
in a much more powerful manner than you can ever work with text output.
The implementation is elegant.
It should be apparent that you could do a lot of typing to
enter commands into PowerShell, make the inevitable mistakes, correct
them, and try again. To make the task a little easier, PowerShell
supports the same kind of command-line editing as the Win32 console
(CMD) does. Some of the more important keys you can use are described
in Table 1.
Table 1. Command editing keystrokes for PowerShell
Keyboard command | Effect |
---|
F2 | Creates
a new command based on your last command. A pop-up screen appears in
which to enter a character. PowerShell then creates a new command,
using the last entered command up to the character you specify. For
example, if the last command is Get-MailboxStatistics –Identity
TRedmond, and you enter F2 followed by c, PowerShell inserts
“Get-MailboxStatistics”. You can then complete the command as you like. |
F4 | Deletes
characters in the current command up to a specified position. For
example, if the cursor is located at the “M” of Get-MailboxStatistics,
and you enter F4 followed by x, PowerShell deletes “Mailbo” and the
result is “Get-xStatistics”. Although this example wouldn’t result in a
useful command, F4 is useful when you need to edit many parameters in a
complex command. |
F7 | Opens a list of the last 50 commands used in the current session to enable you to select a command for reuse. |
F8 | Moves backward through the command history. |
Tab | Requests PowerShell to complete a command based on what you’ve typed. |
Left/Right arrows | Moves the cursor left and right through the current command line. |
Up/Down arrows | Moves up and down through the history of previous commands. |
Delete | Deletes the character under the cursor. |
Insert | Toggles between character insert and character overwrite mode. |
Backspace | Deletes the character before the cursor. |
Most of these keys are straightforward. The two most interesting
keys are F7 and Tab. F7 opens a list of the last 50 commands you have
run in the current session (Figure 1)
so that you can both see what you’ve done in the immediate past and
select one of the commands to re-execute. You can type a couple of
characters into the F7 list, and EMS will look for the first matching
command, or you can use the Up and Down arrows to navigate through the
command history. At times, it’s more convenient to use Up and Down
arrows because you can retrieve more commands and edit a command before
executing it. (F7 selects the command and executes it immediately.)
Windows
PowerShell supports both named and positional parameters. Identifiers
are a good example of a positional parameter. For example, if you enter
Get-Mailbox Tony, PowerShell assumes that Tony is the value for the –Identity parameter.
Finally,
PowerShell completes variables and even the properties of variables
(such as their length) in a way similar to how the Microsoft Visual
Studio IntelliSense feature works. If you type the incomplete name of a
variable and press Tab, PowerShell completes it from the list of known
variables. For example, if you fill a variable with details of a
mailbox as in the following:
$Mailbox = Get-Mailbox –Identity Redmond
and then type $Ma
and press Tab, PowerShell completes it and returns $Mailbox. This is a
useful feature if you forget the names of variables you’ve defined. To
see how properties are completed, type:
$Mailbox.Di
Pressing
Tab now will request PowerShell to go through the list of properties
beginning with Di. For a mailbox, the list is DistinguishedName and
DisplayName.