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, which I'll examine in this section.
Using Windows PowerShell Cmdlets
At the Windows PowerShell prompt, you can get a complete list of cmdlets available by typing get-command
. However, the output
lists both cmdlets and functions by name and definition. With cmdlets,
the definition provided is the syntax, but the full syntax rarely fits
on the line. A better way to get information about cmdlets is to use Get-Help. If you type get-help *-*,
you get a list of all cmdlets, which includes a synopsis that
summarizes the purpose of the cmdlet—which is much more useful than a
list of commands. To get help documentation on a specific cmdlet, type get-help followed by the cmdlet name, such as:
get-help get-variable
Table 1
provides a list of cmdlets you'll commonly use for administration.
Although many other cmdlets are available, these are the ones you're
likely to use the most.
Table 1. Cmdlets Commonly Used for Administration
CMDLET NAME |
DESCRIPTION |
---|
Add-Computer, Remove-Computer |
Adds or removes a computer's membership in a domain or workgroup |
Checkpoint-Computer, Restore-Computer |
Creates a system restore checkpoint for a computer, or restores a computer from a checkpoint |
Compare-Object, Group-Object, Sort-Object, Select-Object, New-Object |
Cmdlets for comparing, grouping, sorting, selecting, and creating objects |
ConvertFrom-SecureString, ConvertTo-SecureString |
Cmdlets for creating or exporting secure strings |
Get-Alias, New-Alias, Set-Alias, Export-Alias, Import-Alias |
Cmdlets for getting, creating, setting, exporting, and importing aliases |
Get-AuthenticodeSignature, Set-AuthenticodeSignature |
Cmdlets for getting or setting the signature object associated with a file |
Get-Command, Invoke-Command, Measure-Command, Trace-Command |
Cmdlets for getting information about cmdlets, invoking commands, measuring the run time of commands, and tracing commands |
Get-Counter |
Gets performance counter data |
Get-Credential |
Gets a credential object based on a password |
Get-Date, Set-Date |
Gets or sets the current date and time |
Get-EventLog, Write-EventLog, Clear-EventLog |
Gets events, writes events, or clears events in an event log |
Get-ExecutionPolicy, Set-ExecutionPolicy |
Gets or sets the effective execution policy for the current shell |
Get-Host |
Gets information about the PowerShell host application |
Get-HotFix |
Gets the Quick Fix Engineering (QFE) updates that have been applied to a computer |
Get-Location, Set-Location |
Displays or sets the current working location |
Get-Process, Start-Process, Stop-Process |
Gets, starts, or stops processes on a computer |
Get-PSDrive, New-PSDrive, Remove-PSDrive |
Gets, creates, or removes a specified PowerShell drive |
Get-Service, New-Service, Set-Service |
Gets, creates, or sets system services |
Get-Variable, New-Variable, Set-Variable, Remove-Variable, Clear-Variable |
Cmdlets for getting, creating, setting, and removing variables as well as for clearing variable values |
Import-Counter, Export-Counter |
Imports or exports performance counter log files |
Limit-EventLog |
Sets the size and age limits for an event log |
New-EventLog, Remove-EventLog |
Creates or removes a custom event log and event source |
Read-Host, Write-Host, Clear-Host |
Reads input from, writes output to or clears the host window |
Add-Computer, Remove-Computer, Stop-Computer, Restart-Computer |
Adds or removes domain membership or stops or restarts a computer |
Reset-ComputerMachinePassword |
Changes and resets the machine account password that the computer uses to authenticate in a domain |
Show-EventLog |
Displays a computer's event logs in Event Viewer |
Show-Service |
Displays a computer's services in the Services utility |
Start-Sleep |
Suspends shell or script activity for the specified period |
Stop-Service, Start-Service, Suspend-Service, Resume-Service, Restart-Service |
Cmdlets for stopping, starting, suspending, resuming, and restarting system services |
Wait-Process |
Waits for a process to be stopped before accepting input |
Write-Output |
Writes an object to the pipeline |
Write-Warning |
Displays a warning message |
All cmdlet parameters are designated with an initial dash (–). To reduce the amount of typing required, some parameters are position-sensitive, so 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. Type get-service fs* to return the status of all Forefront Security–related services.
All cmdlets support the common set of parameters listed in Table 2.
However, 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 2. Common Cmdlet Parameters
PARAMETER NAME |
DESCRIPTION |
---|
Confirm |
Pauses processes, and requires the user to acknowledge the action before continuing. Cmdlets beginning with Remove and Disable 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. |
WarningAction |
Determines how a cmdlet responds to a warning message. Valid values
are SilentlyContinue (suppress the warning and continue), Continue
(display the warning and continue), Inquire (display the warning and
prompt to confirm before continuing), and Stop (display the warning and
halt execution). The default value is Continue. |
WarningVariable |
Sets the name of the variable (in addition to the standard error) in which to store warnings that have occurred. |
WhatIf |
Allows the user to view what would happen if a cmdlet were run with a specific set of parameters. Cmdlets beginning with Remove and Disable have this parameter. |
Understanding Cmdlet Errors
When you work with cmdlets, you'll encounter two standard types of errors:
With both types of errors, you'll typically see error text that can
help you resolve the problem that caused it. For example, an expected
file might be missing or you might not have sufficient permissions to
perform a specified task.
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 3 provides a list of commonly used default aliases. Although there are many other aliases, these are the ones you'll use most frequently.
Table 3. Commonly Used Cmdlet Aliases
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-PSDrive |
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.
|