Running and using cmdlets
Windows PowerShell introduces the concept of a cmdlet (pronounced commandlet).
A cmdlet is the smallest unit of functionality in Windows PowerShell.
You can think of a cmdlet as a built-in command. Rather than being
highly complex, most cmdlets are quite simple and have a small set of
associated properties.
You use cmdlets the same way you use any other commands and
utilities. Cmdlet names are not case sensitive. This means you can use
a combination of both uppercase and lowercase characters. After
starting Windows PowerShell, you can type the name of the cmdlet at the
prompt, and it will run in much the same way as a command-line command.
For ease of reference, cmdlets are named using verb-noun pairs. As Table 1
shows, the verb tells you what the cmdlet does in general. The noun
tells you what specifically the cmdlet works with. For example, the
Get-Variable cmdlet gets a named Windows PowerShell environment
variable and returns its value. If you don’t specify which variable to
get as a parameter, Get-Variable returns a list of all Windows
PowerShell environment variables and their values.
Table 1. Common verbs associated with cmdlets and their meanings
CMDLET VERB
|
USAGE
|
Add |
Adds an instance of an item, such as a history entry or snap-in |
Clear |
Removes the contents of an item, such as an event log or variable value |
New |
Creates a new instance of an item, such as a new mailbox |
Remove |
Removes an instance of an item, such as a mailbox |
Enable |
Enables a setting or mail-enables a recipient |
Disable |
Disables an enabled setting or mail-disables a recipient |
Set |
Modifies specific settings of an object |
Get |
Queries a specific object or a subset of a type of object, such as a specified mailbox or all mailbox users |
You can work with cmdlets in several ways:
-
Executing commands directly at the shell prompt
-
Running commands from scripts
-
Calling them from C# or other .NET Framework languages
You can enter any command or cmdlet you can run at the Windows
PowerShell command prompt into a script by copying the related command
text to a file and saving the file with the .ps1 extension. You can
then run the script in the same way you would any other command or
cmdlet.
Note
Windows PowerShell also includes a rich scripting language and
allows the use of standard language constructs for looping, conditional
execution, flow control, and variable assignment. Discussion of these
features is beyond the scope of this book. A good resource is Windows PowerShell 2.0 Administrator’s Pocket Consultant (Microsoft Press, 2009).
From the Windows command-line environment or a batch script, you can
execute Windows PowerShell cmdlets with the -Command parameter.
Typically, when you do this, you also want to suppress the Windows
PowerShell logo and stop execution of profiles. After doing this, you
can type the following command at a command prompt or insert it into a
.BAT script:
powershell -nologo -noprofile -command get-service
Finally, when you are working with Windows PowerShell, the current
directory is not part of the environment path in most instances.
Because of this, you typically need to use “./” when you run a script
in the current directory, such as:
./runtasks
Running and using other commands and utilities
Because Windows PowerShell runs within the context of the Windows
command prompt, you can run all Windows command-line commands,
utilities, and graphical applications from within Windows PowerShell.
However, remember that the Windows PowerShell interpreter parses all
commands before passing off the command to the command prompt
environment. If Windows PowerShell has a like-named command or a
like-named alias for a command, this command, and not the expected
Windows command, is executed.
Non–Windows PowerShell commands and programs must reside in a
directory that is part of the PATH environment variable. If the item is
found in the path, it is run. The PATH variable also controls where the
Windows PowerShell looks for applications, utilities, and scripts. In
Windows PowerShell, you can work with Windows environment variables
using $env. To view the current settings for the PATH environment
variable, type $env:path. To add a directory to this variable, use the following syntax:
$env:path += ";DirectoryPathToAdd"
where DirectoryPathToAdd is the directory path you want to add to the path, such as:
$env:path += ";C:\Scripts"
To have this directory added to the path every time you start
Windows PowerShell, you can add the command line as an entry in your
profile. Profiles store frequently used elements, including aliases and
functions. Generally speaking, profiles are always loaded when you work
with Windows PowerShell. Keep in mind that cmdlets are like built-in
commands rather than standalone executables. Because of this, they are
not affected by the PATH environment variable.