Working with Cmdlets


Cmdlets provide the basic foundation for working with a computer from within the Windows PowerShell. Although there are many different cmdlets with many different available uses, cmdlets all have common features. I'll examine these common features in this section.

Using Windows PowerShell Cmdlets

At the Windows PowerShell prompt, you can get a complete list of cmdlets available by typing help *-*. To get help documentation on a specific cmdlet, type help followed by the cmdlet name, such as:

help get-variable

Table 6-2 provides a list of cmdlets you'll commonly use for administration. Although there are many other cmdlets available, these are the ones you're likely to use the most.

Table 6-2: Cmdlets Commonly Used for Administration
Open table as spreadsheet

Cmdlet Name

Description

ConvertFrom-SecureString

Export a secure string to a safe format.

ConvertTo-SecureString

Create a securestring from a normal string.

Get-Alias

Returns alias names for cmdlets.

Get-AuthenticodeSignature

Gets the signature object associated with a file.

Get-Credential

Gets a credential object based on a password.

Get-Date

Gets the current date and time.

Get-EventLog

Gets the log data from the Windows log files.

Get-ExecutionPolicy

Gets the effective execution policy for the current shell.

Get-Host

Gets host information.

Get-Location

Displays the current location.

Get-MshDrive

Gets the drive information for the specified Msh drive.

Get-Service

Gets a list of services.

Import-Alias

Imports an alias list from a file.

New-Alias

Creates a new cmdlet-alias pairing.

New-Service

Creates a new service.

Push-Location

Pushes a location to the stack.

Read-Host

Reads a line of input from the host console.

Restart-Service

Restarts a stopped service.

Resume-Service

Resumes a suspended service.

Set-Alias

Maps an alias to a cmdlet.

Set-AuthenticodeSignature

Places an Authenticode signature in a script or other file.

Set-Date

Sets the system date and time on the host system.

Set-ExecutionPolicy

Sets the execution policy for the current shell.

Set-Location

Sets the current working location to a specified location.

Set-Service

Makes and sets changes to the properties of a service.

Start-Service

Starts a stopped service.

Start-Sleep

Suspends shell or script activity for the specified period.

Stop-Service

Stops a running service.

Suspend-Service

Suspends a running service.

Write-Output

Writes an object to the pipeline.

Using Cmdlet Parameters

All cmdlet parameters are designated with an initial dash (-). To reduce the amount of typing required, some parameters are position-sensitive such that you can sometimes pass parameters in a specific order without having to specify the parameter name. For example, with get-service, you don't have to specify the -Name parameter, you can simply type:

Get-service ServiceName

where ServiceName is the name of the service you want to examine, such as:

Get-service MSExchangeIS

This command line returns the status of the Microsoft Exchange Information Store service. Because you can use wildcards, such as *, with name values, you can also type get-service mse* to return the status of all Microsoft Exchange–related services.

All cmdlets support the common set of parameters listed in Table 6-3. However, for you to use these parameters, you must run the cmdlet in such a way that these parameters are returned as part of the result set.

Table 6-3: Common Cmdlet Parameters
Open table as spreadsheet

Parameter Name

Description

-Confirm

Pauses processes and requires the user to acknowledge the action before continuing. Remove- and Disable- cmdlets have this parameter.

-Debug

Provides programming-level debugging information about the operation.

-ErrorAction

Controls the command behavior when an error occurs.

-ErrorVariable

Sets the name of the variable (in addition to the standard error) in which to place objects for which an error has occurred.

-OutBuffer

Sets the output buffer for the cmdlet.

-OutVariable

Sets the name of the variable in which to place output objects.

-Verbose

Provides detailed information about the operation.

-WhatIf

Allows the user to view what would happen if a cmdlet were run with a specific set of parameters. Remove- and Disable- cmdlets have this parameter.

Understanding Cmdlet Errors

When you work with cmdlets, you'll encounter two standard types of errors:

  • Terminating errors Errors that halt execution

  • Nonterminating errors Errors that cause error output to be returned but do not halt execution

With both types of errors, you'll typically see error text that can help you resolve the problem. For example, an expected file might be missing, or you may not have sufficient permissions to perform a specified task.

Using Cmdlet Aliases

For ease of use, Windows PowerShell lets you create aliases for cmdlets. An alias is an abbreviation for a cmdlet that acts as a shortcut for executing the cmdlet. For example, you can use the alias gsv instead of the cmdlet name get-service.

Table 6-4 provides a list of commonly used default aliases. Although there are many other aliases, these are the ones you'll use most frequently.

Table 6-4: Commonly Used Cmdlet Aliases
Open table as spreadsheet

Alias

Cmdlet

clear, cls

Clear-Host

Diff

Compare-Object

cp, copy

Copy-Item

Epal

Export-Alias

Epcsv

Export-Csv

Foreach

ForEach-Object

Fl

Format-List

Ft

Format-Table

Fw

Format-Wide

Gal

Get-Alias

ls, dir

Get-ChildItem

Gcm

Get-Command

cat, type

Get-Content

h, history

Get-History

gl, pwd

Get-Location

gps, ps

Get-Process

Gsv

Get-Service

Gv

Get-Variable

Group

Group-Object

Ipal

Import-Alias

Ipcsv

Import-Csv

R

Invoke-History

Ni

New-Item

Mount

New-MshDrive

Nv

New-Variable

rd, rm, rmdir, del, erase

Remove-Item

Rv

Remove-Variable

Sal

Set-Alias

sl, cd, chdir

Set-Location

sv, set

Set-Variable

Sort

Sort-Object

Sasv

Start-Service

Sleep

Start-Sleep

spps, kill

Stop-Process

Spsv

Stop-Service

write, echo

Write-Output

You can define additional aliases using the Set-Alias cmdlet. The syntax is:

Set-alias aliasName cmdletName

Where aliasName is the alias you want to use and cmdletName is the cmdlet for which you are creating an alias. The following example creates a "go" alias for the get-process cmdlet:

Set-alias go get-process

To use your custom aliases whenever you work with Windows PowerShell, enter the related command line in your profile.




Microsoft Exchange Server 2007 Administrator's Pocket Consultant
Microsoft Exchange Server 2007 Administrators Pocket Consultant Second Edition
ISBN: 0735625867
EAN: 2147483647
Year: 2007
Pages: 119

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net