Displaying available command types in Windows PowerShell

10/20/2009

The Get-Command command does not list every command that is available in Windows PowerShell. Instead, the Get-Command command lists only the cmdlets in the Windows PowerShell session. Windows PowerShell actually supports several other types of commands. Aliases, functions, and scripts are also Windows PowerShell commands, although they are not discussed in detail in the Windows PowerShell Primer. External files that are executables, or have a registered file type handler, are also classified as commands.

You can return a listing of all items that can be invoked by entering the following command:

PS> Get-Command *

Because this list includes external files in your search path, it may contain thousands of items. It is more useful to look at a reduced set of commands. To find native commands of other types, you can use the CommandType parameter of the Get-Command cmdlet. Although we have not talked about these other command types yet, you can still display them if you know the name of the CommandType for a class of commands.

Note:

Although we have not discussed it yet, the asterisk (*) is used for wildcard matching in Windows PowerShell command arguments. The * means "match one or more of any characters". You can type Get-Command a* to find all commands that begin with the letter "a". Unlike wildcard matching in Cmd.exe, Windows PowerShell's wildcard will also match a period.

To display the special command category aliases (these are nicknames used as alternatives to standard command names), enter the following command:

PS> Get-Command -CommandType Alias

To display all Windows PowerShell functions, enter the following command:

PS> Get-Command -CommandType Function

To display external scripts in Windows PowerShell's search path, enter the following command:

PS> Get-Command -CommandType ExternalScript

Posted in: MS Windows| Tags: Windows PowerShell Cmdlets Windows PowerShell get-command Syntax asterisk match CommandType CommandType Function CommandType ExternalScript CommandType Alias

Installing and Running Windows PowerShell

10/20/2009

Installation Requirements

Before you install Windows PowerShell, be sure that your system has the software programs that Windows PowerShell requires. Windows PowerShell requires the following programs:

· Windows XP Service Pack 2, Windows 2003 Service Pack 1, or later versions of Windows

· Microsoft .NET Framework 2.0

If any version of Windows PowerShell is already installed on the computer, use Add or Remove Programs in Control Panel to uninstall it before installing a new version.

Installing Windows PowerShell

To install Windows PowerShell:

1. Download the Windows PowerShell installation file. (The name of the file will differ with the platform, operating system, and language pack.)

2. To start the installation, click Open.

3. Follow the instructions on the installation wizard pages.

You can also save the Windows PowerShell files to a network share for installation on multiple computers.

To perform a silent installation, type:

<PowerShell-exe-file-name> /quiet

For example,

PowerShellSetup_x86_fre.exe /quiet

On 32-bit versions of Windows, Windows PowerShell is installed, by default, in the %SystemRoot%\System32\WindowsPowerShell\v1.0  directory. On 64-bit versions of Windows, a 32-bit version of Windows PowerShell is installed in the %SystemRoot%\SystemWow64\WindowsPowerShell\v1.0 directory and a 64-bit version of Windows PowerShell is installed in the %SystemRoot%\System32\WindowsPowerShell\v1.0 directory.

Running Windows PowerShell

To start Windows PowerShell from the Start Menu, click Start, click All Programs, click Windows PowerShell 1.0, and then click the Windows PowerShell icon.

To start Windows PowerShell from the Run box, click Start, click Run, type powershell, and click OK.

To start Windows PowerShell from a Command Prompt (cmd.exe) window, at the command prompt, type powershell. Because Windows PowerShell runs in a console session, you can use this same technique to run it within a remote telnet or SSH session. To return to your Command Prompt session, type exit.

Windows PowerShell Basics

Graphical interfaces use some basic concepts that are well known to most computer users. Users rely on the familiarity of those interfaces to to accomplish tasks. Operating systems present users with a graphical representation of items that can be browsed, usually with drop-down menus for accessing specific functionality and context menus for accessing context-specific functionality.

A command-line interface (CLI), such as Windows PowerShell, must use a different approach to expose information, because it does not have menus or graphical systems to help the user. You need to know command names before you can use them. Although you can type complex commands that are equivalent to the features in a GUI environment, you must become familiar with commonly-used commands and command parameters.

Most CLIs do not have patterns that can help the user to learn the interface. Because CLIs were the first operating system shells, many command names and parameter names were selected arbitrarily. Terse command names were generally chosen over clear ones. Although help systems and command design standards are integrated into most CLIs, they have been generally designed for compatibility with the earliest commands, so the command set is still shaped by decisions made decades ago.

Windows PowerShell was designed to take advantage of a user's historic knowledge of CLIs. In this chapter, we will talk about some basic tools and concepts that you can use to learn Windows PowerShell quickly. They include:

· Using Get-Command

· Using Cmd.exe and UNIX commands

· Using External Commands

· Using Tab-Completion

· Using Get-Help

Posted in: MS Windows| Tags: Windows PowerShell Windows PowerShell get-command get-help Installation Requirements Installation PowerShell-exe-file-name quiet Windows PowerShell Basics CLI command-line interface Tab-Completion Cmd.exe UNIX commands

About Windows PowerShell

10/20/2009

Windows PowerShell is designed to improve the command-line and scripting environment by eliminating long-standing problems and adding new features.

Discoverability

Windows Powershell makes it easy to discover its features. For example, to find a list of cmdlets that view and change Windows services, type:

get-command *-service

After discovering which cmdlet accomplishes a task, you can learn more about the cmdlet by using the Get-Help cmdlet. For example, to display help about the Get-Service cmdlet, type:

get-help get-service

To fully understand the output of that cmdlet, pipe its output to the Get-Member cmdlet. For example, the following command displays information about the members of the object output by the Get-Service cmdlet.

get-service | get-member

Consistency

Managing systems can be a complex endeavor and tools that have a consistent interface help to control the inherent complexity. Unfortunately, neither command-line tools nor scriptable COM objects have been known for their consistency.

The consistency of Windows PowerShell is one of its primary assets. For example, if you learn how to use the Sort-Object cmdlet, you can use that knowledge to sort the output of any cmdlet. You do not have to learn the different sorting routines of each cmdlet.

In addition, cmdlet developers do not have to design sorting features for their cmdlets. Windows PowerShell gives them a framework that provides the basic features and forces them to be consistent about many aspects of the interface. The framework eliminates some of the choices that are typically left to the developer, but, in return, it makes the development of robust and easy-to-use cmdlets much simpler.

Interactive and Scripting Environments

Windows PowerShell is a combined interactive and scripting environment that gives you access to command-line tools and COM objects, and also enables you to use the power of the .NET Framework Class Library (FCL).

This environment improves upon the Windows Command Prompt, which provides an interactive environment with multiple command-line tools. It also improves upon Windows Script Host (WSH) scripts, which let you use multiple command-line tools and COM automation objects, but do not provide an interactive environment.

By combining access to all of these features, Windows PowerShell extends the ability of the interactive user and the script writer, and makes system administration more manageable.

Object Orientation

Although you interact with Windows PowerShell by typing commands in text, Windows PowerShell is based on objects, not text. The output of a command is an object. You can send the output object to another command as its input. As a result, Windows PowerShell provides a familiar interface to people experienced with other shells, while introducing a new and powerful command-line paradigm. It extends the concept of sending data between commands by enabling you to send objects, rather than text.

Easy Transition to Scripting

Windows PowerShell makes it easy to transition from typing commands interactively to creating and running scripts. You can type commands at the Windows PowerShell command prompt to discover the commands that perform a task. Then, you can save those commands in a transcript or a history before copying them to a file for use as a script.

Posted in: MS Windows| Tags: Windows PowerShell Windows PowerShell Get-Date get-command get-help get-service get-member Consistency FCL Framework Class Library Scripting Environments Object Orientation transition

Array and Array Operations in Windows PowerShell

10/20/2009

Array Operations

Does this array have a 3 in it

1,2,3,5,3,2 –contains 3

Return all elements equal to 3:

1,2,3,5,3,2 –eq 3

Return all elements less than 3:

1,2,3,5,3,2 –lt 3

Test if 2 exists in collection:

if (1, 3, 5 –contains 2) …

Other operators: -gt, -le, -ge, -ne

Arrays

“a”,“b”,”c”

array of strings

1,2,3

array of integers

@()

empty array

@(2)

array of 1 element

1,(2,3),4

array within array

,”hi”

Array of one element

$a[5]

sixth element of array*

$a[2][3]

fourth element or the third

 

element of an array

$a[2..20]

Return elements 3 thru 21

· Arrays are zero based.

Posted in: MS Windows Software Programming| Tags: Windows PowerShell Windows PowerShell Array Array Operations

Hot Posts

Latest posts

Tags

Others

Sponsors