WindowsPowerShell
By Bruce Payette
CONTENTS INCLUDE:
n
About Windows Powershell
n
The Language
n
Operators
n
Basic Tasks—Text and Files
n
Types and Objects
n
Building Custom Objects
n
Hot Tips and more...
DZone, Inc.
|
www.dzone.com
ABOUT WINDOWS POWERSHELL
Windows PowerShell
www.dzone.com Get More Refcardz! Visit refcardz.com
PowerShell is freely available through the Microsoft Windows
Update Service packaged as an optional update for Windows
XP SP2, Windows Vista and Windows Server 2003. It is also
included with Windows Server 2008 as an optional component.
Once installed, it can be started from the Start menu or simply
by running “powershell.exe”. Basic things you need to know:
n
Use the “exit” keyword to exit the shell
n
Ctrl-C will interrupt the current task returning you to the prompt
n
A command can be spread over multiple lines and the
interpreter will prompt for additional input. The line continuation
character is the back-quote ‘`’ (also called the back-tick).
n
To get help about a command you can do “help command”.
The help command by itself will give you a list of topics.
n
The help command supports wildcards so “help get-*”
will return all of the commands that start with “get-”.
n
You can also get basic help on a command by doing
“commandName -?” like “dir -?”
n
As well as cmdlet help, there is a collection of general help
topics prefixed with “about_”. You can get a list of these
topics by going help about_*
Command-Line editing in Powershell: Command-line Editing
works just like it does in cmd.exe: use the arrow keys to go up
and down, the insert and delete keys to insert and delete
characters and so on.
GETTING STARTED WITH POWERSHELL
n
Authoritative content
n
Designed for developers
n
Written by top experts
n
Latest tools & technologies
n
Hot tips & examples
n
Bonus content online
n
New issue every 1-2 weeks
Subscribe Now for FREE!
Refcardz.com
Get More Refcardz
(
They’re free!
)
tech facts at your fingertips
Method Name Parameters and Descriptions
open(method, url, async) open a connection to a URL
method = HTTP verb (GET, POST, etc.)
url = url to open, may include querystring
async = whether to make asynchronous request
onreadystatechange assign a function object as callback (similar to onclick,
onload, etc. in browser event model)
setRequestHeader
(namevalue)
add a header to the HTTP request
send(body) send the request
body = string to be used as request body
abort() stop the XHR from listening for the response
readyState stage in lifecycle of response (only populated after send()
is called)
httpStatus The HTTP return code (integer, only populated after
response reaches the loaded state)
responseText body of response as a JavaSc ript string (only set afte r
response reaches the interacti ve readyState)
responseXML body of the response as a XML document object (only
set after response reaches the interactive readyState)
getResponseHeader
(name)
read a response header by name
getAllResponseHeaders() Get an array of all response header names
Hot
Tip
tech facts at your fingertips
Keyboard
sequence
Editing operation
Left/Right Arrows Move the editing cursor left and right through the current
command line.
Ctrl-Left Arrow,
Ctrl-Right Arrow
Move the editing cursor left and right a word at a time.
Home Move the editing cursor to the beginning of the current
command line.
End Move the editing cursor to the end of the current command line.
Up/Down Arrows Move up and down through the command history.
Insert Key Toggles between character insert and character overwrite modes.
Delete Key Deletes the character under the cursor
Backspace Key Deletes the character behind the cursor.
F7 Pops up command history in a window on the console. Use
the up and down arrows to select a command then Enter to
execute that command.
Tab Does command line completion. PowerShell completes on
filenames, cmdlet names (after the dash), cmdlet parameter
names and property and method names on variables.
THE LANGUAGE
PowerShell parses text in one of two modes—command mode,
where quotes are not required around a string and expression
mode where strings must be quoted. The parsing mode is
determined by what’s at the beginning of the statement. If it’s a
command, then the statement is parsed in command mode. If
it’s not a command then the statement is parsed in expression
mode as shown:
PS (1) > echo 2+2 Hi there # command mode – starts with
‘echo’ command
2+2 Hi there
PS (2) > 2+2; “Hi there” # expression mode starts with 2
4
Hi there
PS (3) > echo (2+2) Hi (echo there) # Mixing and matching
modes with brackets)
4 Hi there
Why PowerShell? Why Now? PowerShell was designed to do for
Windows what the UNIX shells do for UNIX: provide a powerful,
well-integrated command-line experience for the operation
system. Unfortunately since Windows is mostly managed through
objects (WMI, COM and .NET) this required creating a new kind
of shell. So why create it now? As Windows moves off the desktop
and into server farms or application servers like print, DNS
and LDAP services, command-line automation becomes a
fundamental requirement.
This refcard covers starting and using Windows PowerShell,
including the syntax for all statements, operators and other
elements of the language. Also included are examples of how
to use .NET, COM, ADSI and WMI objects from PowerShell.
Finally, it includes tips and tricks—short examples showing
how to perform common tasks from PowerShell.
#5