Applications Server
 

Exchange Server 2013 : The Exchange Management Shell - EMS basics (part 1) - Command editing

12/1/2013 8:13:36 PM

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*.

Inside Out Getting to know the cmdlets

You will probably begin by using the /full switch to retrieve all available help for a cmdlet to get to know what each cmdlet does. After you learn more about the cmdlet, you can move on to the default view as you become more accustomed to working with EMS. Remember that the Exchange help file contains information about all the EMS cmdlets. The advantage of using the help file (which is always present on a server) is that you can use the help file’s index to search for specific entries.

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.

1. Command editing

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.)

A screen shot of EMS, showing how the F7 key recalls commands previously issued in the session.

Figure 1. Using F7 to recall EMS commands

Inside Out An easy way to type a command

Tab completion is a wonderful feature that Windows PowerShell inherited from CMD. You can partially enter a command and then press Tab to have PowerShell fill in the rest of the cmdlet name followed by its parameters. For example, type:

Get-Dist

This isn’t the name of a valid cmdlet, but it is the root of several cmdlets, so when you press Tab, PowerShell completes the first valid cmdlet that matches and inserts:

Get-DistributionGroup

If you press Tab again, PowerShell moves to the next cmdlet that matches and inserts:

Get-DistributionGroupMember

If you press Tab again, PowerShell returns to Get-DistributionGroup because there are only two valid matches. PowerShell also supports completion for parameters. If you insert a dash to indicate a parameter value after Get-DistributionGroup and press Tab, PowerShell starts with the first parameter and continues through all valid parameters. If you press Tab too many times and pass the parameter you want to use, you can press Shift+Tab to go back through the parameter list. If you add some characters to help PowerShell identify the parameter, it attempts to complete using that value. For example:

PowerShell completes Get-DistributionGroup –Ma into the command Get-DistributionGroup –ManagedBy.

Even better, tab completion is context-sensitive, so it understands the structure of the object you are navigating. For example, if you want to move through the system registry, tab completion understands the hive structure, so you can type a location in the registry and then use the Tab key to move through the available choices from that point. For example, type:

CD HKLM:\Software\Microsoft\Exchange

Now press Tab, and PowerShell leads you through all the registry locations Exchange uses.

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.

 
Others
 
- Exchange Server 2013 : The Exchange Management Shell - Using remote Windows PowerShell - Connecting to remote PowerShell
- Exchange Server 2013 : The Exchange Management Shell - How Exchange uses Windows PowerShell
- Active Directory Planning and Installation : Installing Active Directory - Promoting a Domain Controller
- Active Directory Planning and Installation : Understanding Domain and Forest Functionality
- Active Directory Planning and Installation : Verifying Network Connectivity - Tools and Techniques for Testing Network Configuration
- Active Directory Planning and Installation : Verifying the Filesystem - Setting Up the NTFS Partition
- Sharepoint 2013 : Building an Application with Access Services (part 6) - Adding a Macro, Reporting and External Data
- Sharepoint 2013 : Building an Application with Access Services (part 5) - Modifying Application Views, Creating a Query
- Sharepoint 2013 : Building an Application with Access Services (part 4) - Adding, Removing, and Editing Tables
- Sharepoint 2013 : Building an Application with Access Services (part 3) - Creating the Basic Application
- Sharepoint 2013 : Building an Application with Access Services (part 2) - Configuring SQL Server 2012, Configuring the Windows Development Environment Firewall
- Sharepoint 2013 : Building an Application with Access Services (part 1) - Configuring an On-premise Development Environment
- Microsoft Lync Server 2013 : Lync Online and Hybrid Deployments - Configuring Directory Synchronization (part 1)
- Microsoft Lync Server 2013 : Lync Online and Hybrid Deployments - Configuring Directory Synchronization (part 1)
- Microsoft Lync Server 2013 : Lync Online and Hybrid Deployments - AD FS Deployment for SSO (part 3) - Adding or Converting a Domain for SSO
- Microsoft Lync Server 2013 : Lync Online and Hybrid Deployments - AD FS Deployment for SSO (part 2)
- Microsoft Lync Server 2013 : Lync Online and Hybrid Deployments - AD FS Deployment for SSO (part 1) - Configuring the First Federation Server in the Farm
- Sharepoint 2013 : Managing and Configuring Communities
- Sharepoint 2013 : Managing and Configuring My Sites (part 4) - SkyDrive Pro
- Sharepoint 2013 : Managing and Configuring My Sites (part 3) - Configuring My Sites - Managing Social Tags and Notes, Manage Following
 
 
Most View
 
- Using the Windows 8 Interface : Navigating the Start Screen (part 1) - Navigating the Start Screen with a Mouse
- Windows 8 : Creating a Windows Network - Installing Network Wiring (part 1) - General Cabling Tips
- Windows Server 2008 : Launching the Group Policy Management Console, Understanding Group Policy Order of Precedence
- Windows Server 2012 : Deploying Dynamic Access Control (part 1) - Preparing Claims
- Overview of Oauth in Sharepoint 2013 : Introduction to OAuth
- Windows 7 : The WMC Functions (part 2) - Pictures, Videos, Music
- Adobe Illustrator CS5 : Working with Paths (part 2) - Understanding control handles and control handle lines
- Windows Server 2008 : Using netdom (part 2) - Verifying Trust Relationships
- : Windows Server 2008 and Windows Vista : Administrative (.adm) Templates (part 4) - Managing .adm Templates
- Windows Server 2012 : Hyper-V - Creating and Configuring Virtual Machines (part 2) - Creating Virtual Machines
 
 
Top 10
 
- Microsoft Excel 2010 : Filtering Options (part 3) - Using the Search Function for Grouped Dates, Using Text, Number and Date Special Filters
- Microsoft Excel 2010 : Filtering Options (part 2) - Searching Functions for Listed Items
- Microsoft Excel 2010 : Filtering Options (part 1) - Filter Listing for Listed Items, Grouped Dates Filter Listing
- Microsoft Excel 2010 : Filtering and Consolidating Data - Preparing Data, Applying a Filter to a Dataset
- Microsoft PowerPoint 2010 : Inserting Charts and Related Material - Formatting a SmartArt Graphic
- Microsoft PowerPoint 2010 : Inserting Charts and Related Material - Resizing a SmartArt Graphic
- Microsoft PowerPoint 2010 : Inserting Charts and Related Material - Modifying a SmartArt Graphic
- Microsoft PowerPoint 2010 : Inserting Charts and Related Material - Using the Text Pane with SmartArt Graphics
- Microsoft PowerPoint 2010 : Inserting Charts and Related Material - Creating SmartArt Graphics
- Microsoft Lync Server 2013 : SQL Server Dependencies - Creating SQL Server Maintenance Plans