PowerShell ISE
Another new feature that
was introduced in PowerShell 2.0 is called the Integrated Scripting
Environment (ISE). The ISE, as shown in Figure 1, is a Windows Presentation
Foundation (WPF)–based host application for Windows PowerShell. Using
the ISE, an IT professional can both run commands and write, test, and
debug scripts.
Additional features of the
ISE include the following:
A Command pane for running interactive
commands.
A Script
pane for writing, editing, and running scripts. You can run the entire
script or selected lines from the script.
A scrollable Output pane that displays a
transcript of commands from the Command and Script panes and their
results.
Up
to eight independent PowerShell execution environments in the same
window, each with its own Command, Script, and Output panes.
Multiline editing in the
Command pane, which lets you paste multiple lines of code, run them, and
then recall them as a unit.
A built-in debugger for debugging commands, functions, and
scripts.
Customizable
features that let you adjust the colors, font, and layout.
A scriptable object model
that lets you further customize and extend the PowerShell ISE.
Line and column numbers,
keyboard shortcuts, tab completion, context-sensitive Help, and Unicode
support.
The PowerShell ISE is an optional
feature in Windows Server 2008 R2. To use the ISE, it first must be
installed using the Add Features Wizard. Because the ISE requires the
.NET Framework 3.5 with Service Pack 1, the Server Manager will also
install this version of the .NET Framework if it is not already
installed. Once installed, use either of the following methods to start
it:
1. | Start
Windows PowerShell ISE by clicking Start, All Programs, Accessories,
Windows PowerShell, and then click Windows PowerShell ISE or Windows
PowerShell ISE (x86).
|
2. | Or execute the powershell_ise.exe executable.
|
ISE Requirements
The following requirements must
be met to use the ISE:
Note
Being a GUI-based application,
the PowerShell ISE does not work on Server Core installations of Windows
Server.
Variables
A variable is a storage place for data. In most
shells, the only data that can be stored in a variable is text data. In
advanced shells and programming languages, data stored in variables can
be almost anything, from strings to sequences to objects. Similarly,
PowerShell variables can be just about anything.
To define a
PowerShell variable, you must name it with the $ prefix, which helps
delineate variables from aliases, cmdlets, filenames, and other items a
shell operator might want to use. A variable name can contain any
combination of alphanumeric characters (a–z and 0–9) and the underscore
(_) character. Although PowerShell variables have no set naming
convention, using a name that reflects the type of data the variable
contains is recommended, as shown in this example:
PS C:\> $Stopped = get-service | where {$_.status -eq "stopped"}
PS C:\> $Stopped
Status Name DisplayName
------ ---- -----------
Stopped ALG Application Layer Gateway Service
Stopped Appinfo Application Information
Stopped AppMgmt Application Management
Stopped aspnet_state ASP.NET State Service
Stopped AudioEndpointBu... Windows Audio Endpoint Builder
Stopped Audiosrv Windows Audio
...
As you can see from the
previous example, the information that is contained within the $Stopped variable is a collection of services that are
currently stopped.
Note
A variable name can consist
of any characters, including spaces, provided the name is enclosed in
curly braces ({ and } symbols).
Aliases
Like most existing
command-line shells, command aliases can be defined in PowerShell.
Aliasing is a method that is used to execute existing shell commands
(cmdlets) using a different name. In many cases, the main reason aliases
are used is to establish abbreviated command names in an effort to
reduce typing. For example:
PS C:\> gps | ? {$_.Company -match ".*Microsoft*"} | ft Name, ID, Path –Autosize
The preceding example shows
the default aliases for the Get-Process, Where-Object, and Format-Table
cmdlets.
Alias cmdlets
In
PowerShell, several alias cmdlets enable an administrator to define new
aliases, export aliases, import aliases, and display existing aliases.
By using the following command, an administrator can get a list of all
the related alias cmdlets:
PS C:\> get-command *-Alias
CommandType Name Definition
----------- ---- ----------
Cmdlet Export-Alias Export-Alias [-Path] <String...
Cmdlet Get-Alias Get-Alias [[-Name] <String[]...
Cmdlet Import-Alias Import-Alias [-Path] <String...
Cmdlet New-Alias New-Alias [-Name] <String> [...
Cmdlet Set-Alias Set-Alias [-Name] <String> [...
Use the Get-Alias cmdlet to
produce a list of aliases available in the current PowerShell session.
The Export-Alias and Import-Alias cmdlets are used to export and import
alias lists from one PowerShell session to another. Finally, the
New-Alias and Set-Alias cmdlets allow an administrator to define new
aliases for the current PowerShell session.
Creating Persistent
Aliases
The aliases created when
using the New-Alias and Set-Alias cmdlets are valid only in the current
PowerShell session. Exiting a PowerShell session discards any existing
aliases. To have aliases persist across PowerShell sessions, they can be
defined in a profile file, as shown in this example:
set-alias new new-object
set-alias time get-date
...
Although command
shortening is appealing, the extensive use of aliases isn’t recommended.
One reason is that aliases aren’t very portable in relation to scripts.
For example, if a lot of aliases are used in a script, each alias must
be included via a Set-Aliases sequence at the start of the script to
make sure those aliases are present, regardless of machine or session
profile, when the script runs.
However, a bigger
concern than portability is that aliases can often confuse or obscure
the true meaning of commands or scripts. The aliases that are defined
might make sense to a scripter, but not everyone shares the logic in
defining aliases. So if a scripter wants others to understand their
scripts, they shouldn’t use too many aliases.
Note
If aliases will be used in a
script, use names that other people can understand. For example, there’s
no reason, other than to encode a script, to create aliases consisting
of only two letters.