24/7/365 Support

Windows

Work with Each Item in a List or Windows PowerShell Command Output

Problem

You have a list of items and want to work with each item in that list.

Solution

Use the ForeachObject cmdlet (which has the standard aliases foreach and %)to work with each item in a list.

To apply a calculation to each item in a list, use the $_ variable as part of a calculation in the scriptblock parameter:

PS >1..10 | ForeachObject { $_ * 2 } 2 4 6 8 10 12 14 16 18 20

To run a program on each file in a directory, use the $_ variable as a parameter to the program in the script block parameter:

Program: Interactively Filter Lists of Objects in Windows PowerShell

There are times when the WhereObject cmdlet is too powerful. In those situations, the CompareProperty script provides a much simpler alternative. There are also times when the WhereObject cmdlet is too simple—when expressing your selection logic as code is more cumbersome than selecting it manually. In those situations, an interactive filter can be much more effective.

yet in the book, so feel free to just consider it a neat script for now. To learn more about a part that you don’t yet understand, look it up in the table of contents or the index.

Program: Simplify Most Where-Object Filters in Windows PowerShell

The WhereObject cmdlet is incredibly powerful, in that it allows you to filter your output based on arbitrary criteria. For extremely simple filters (such as filtering based only on a comparison to a single property), though, the syntax can get a little ungainly:

GetProcess | WhereObject { $_.Handles gt 1000 }

load all the syntax to the script itself:

GetProcess | CompareProperty Handles gt 1000 GetChildItem | CompareProperty PsIsContainer

With a shorter alias, this becomes even easier to type:

Filter Items in a List or Windows PowerShell Command Output

Problem

You want to filter the items in a list or command output.

Solution

Use the WhereObject cmdlet (which has the standard aliases, where and ?) to select items in a list (or command output) that match a condition you provide.

To list all running processes that have "search" in their name, use the like operator to compare against the process’s Name property:

GetProcess | WhereObject { $_.Name like "*Search*" }

To list all directories in the current location, test the PsIsContainer property:

Pipelines in Windows PowerShell

One of the fundamental concepts in a shell is called the pipeline. It also forms the basis of one of the most significant advances that PowerShell brings to the table. A pipeline is a big name for a simple concept—a series of commands where the output of one becomes the input of the next. Apipeline in a shell is much like an assembly line in a factory: it successively refines something as it passes between the stages.

Example 21. A PowerShell pipeline

Use Console Files to Load and Save Sets of Snapins in Windows PowerShell

Problem

You want to load PowerShell with a set of additional snapins, but do not want to modify your (or the user’s) profile.

Solution

Once you register a snapin on your system, you can add its snapin identifier to a PowerShell console file to load it. When you specify that file as the PsConsoleFile parameter of PowerShell.exe, PowerShell loads all snapins defined by the console file into the new session.

Save the list of currently loaded snapins to a console file:

ExportConsole Filename.psc1

Extend Your Windows PowerShell with Additional Snapins

Problem

You want to use PowerShell cmdlets and providers written by a third party.

Solution

In PowerShell, extensions that contain additional cmdlets and providers are called snapins. The author might distribute them with an automated installer but can also distribute them as a standalone PowerShell assembly. PowerShell identifies each snapin by the filename of its assembly and by the snapin name that its author provides.

To use a snapin:

Windows PowerShell Configure Debug, Verbose, and Progress Output

Problem

You want to manage the detailed debug, verbose, and progress output generated by cmdlets and scripts.

Solution

To enable debug output for scripts and cmdlets that generate it:

$debugPreference = "Continue" StartDebugCommand

To enable verbose mode for a cmdlet that checks for the Verbose parameter:

CopyItem c:\temp\*.txt c:\temp\backup\ Verbose

To disable progress output from a script or cmdlet that generates it:

$progressPreference = "SilentlyContinue" GetProgress.ps1

Discussion

Manage the Error Output of Windows PowerShell Commands

Problem

You want to display detailed information about errors that come from commands.

Solution

To list all errors (up to $MaximumErrorCount) that have occurred in this session, access the $error array:

$error

To list the last error that occurred in this session, access the first element in the $error array:

$error[0]

To list detailed information about an error, pipe the error into the FormatList cmdlet with the Force parameter:

$currentError = $error[0]

$currentError | FormatList Force

Display the Properties of an Item As a Table in Windows PowerShell

Problem

You have a set of items (for example, error records, directory items, or .NET objects), and you want to display summary information about them in a table format.

Solution

To display summary information about a set of items, pass those items to the FormatTable cmdlet. This is the default type of formatting for sets of items in PowerShell and provides several useful features.

To use PowerShell’s default formatting, pipe the output of a cmdlet (such as the GetProcess cmdlet) to the FormatTable cmdlet:

GetProcess | FormatTable

Pages

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say