Windows PowerShell Basics

Windows PowerShell Basics

  • Comments 2
  • Likes

Overview

Everyone should be aware by now that Windows PowerShell is the latest scripting environment for managing and maintaining Windows servers, workstations and applications. This cube note provides a brief introduction to PowerShell resources and a few commands to get started with.

Where to get Windows PowerShell

If you have Windows Server 2008, you can enable Windows PowerShell as an additional feature. For Windows XP, Windows Vista or Windows Server 2003 you can download it here. The documentation pack for Windows PowerShell is available here.

Providers

PowerShell providers allow the scripting environment to access the APIs of various components through a common interface, much like WMI providers do. A listing of the currently loaded providers can be found using the get-PSProvider command. The resource exposed by PowerShell providers can be accessed like a virtual drive. The get-PSDrive command will list the currently available drives. You can navigate to the drive with the cd <drive>: command (be sure to use the : at the end). You can list the contents of the current virtual drive path with the dir command.

Additional documentation about providers can be found with the command help about_Provider. Type help about_path_syntax for more information about the navigating PowerShell drives.

Cmdlets

PowerShell commands, called cmdlets, are written in a verb-object format. A list of the currently available commands can be found by typing the cmdlet: get-command. In addition to the default cmdlets, additional cmdlets can be added when a new provider is loaded. Custom cmdlets can be written and loaded as part of the user's personal profile.

Type help <any cmdlet name here> to get a description of the cmdlet and the available parameters. You can also type help <cmdlet name> -full to get detailed documentation about the cmdlet.

Aliases

The alias functionality in PowerShell allows users to avoid typing long cmdlets names for commonly used commands. A list of the default aliases can be found by typing get-alias. New, custom aliases can be written using the new-alias cmdlet

Pipelining

The output of most cmdlets is an array of objects that can be piped as the input of a second cmdlet. The | symbol is required to separate cmdlet expressions. The two most common cmdlets that output is piped to are the Where-Object (alias where) and Sort-Object (alias sort).

The Sort cmdlet takes an object's properties as parameters. If a collection of objects is piped to the sort cmdlet, the output can be sorted by one or more properties

Sort Cmdlet Example (try this yourself and note the differences)

· Get-Service

· Get-Service | sort status

· Get-Service | sort status, name

Where Cmdlet Example

The Where cmdlet will filter the output of the preceding cmdlet. It does not remove objects from the collection, but reduces the number of objects displayed. The syntax to filter the Get-Service cmdlet to display only running services would look like this:

Get-Service | Where-object {$_.status -like "running"}

The parameters for the where-object cmdlet is a code block contained by curly braces {}.

The $_ variable is a special built-in variable representing the collection of objects piped from the previous cmdlet.

The .status property of the piped objects is what we will be filtering on.

-like is the comparison operator we are using.

"Running" is the value we are using to filter the .status property.

In my next post we will explore variables, regular expressions, comparison operators and formatting output. The following example will explore code blocks and advanced topics.

Explore on your own:

· Install or enable PowerShell.

· Run the get-PSProvider cmdlet and the get-PSDrive cmdlet to see what is currently available.

· Use the dir and CD commands to navigate the PSDrives on your workstation.

· Type get-command to view the cmdlets currently available. Now type get-command new-* to just view the cmdlets starting with New. How is this different from get-command new*.

· Type help get-WMIObject -full. Observe the available parameters and usage examples. Practice using the cmdlet to explore the some common classes obtained by hardware inventory in SMS.

· What cmdlets are the following aliases for: dir, CD, help, where, sort?

· Pipe the results of the get-alias cmdlet to the sort-object cmdlet to sort by definition. Observe the various aliases for get-childitem and remove-item. Now pipe get-alias to the where-object cmdlet to filter by definitions ending with the string -object and then pipe that to the sort-object cmdlet.

Additional PowerShell resources

· Windows Powershell home page

· Scripting with Windows Powershell

· Sample Powershell Scripts for managing servers and applications

Richard Pesenko | Support Escalation Engineer

Comments
  • Overview: In the previous lesson , we looked at use of the Where-Object cmdlet.&#160; The example we

  • Overview In lesson 1 and lesson 2 we looked at the Where-Object cmdlet.&#160; The example we looked at

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment