Finding Files in the Path – A Pipeline Perk

I’ve been chipping in on a forum post about finding if a given file exists in any folder within the system environmental %PATH% variable using Windows PowerShell. There are several ways you might approach this. But the best way in my opinion is to leverage the PowerShell pipeline. Perhaps you don’t really need the solution but I think it is a valuable learning opportunity. Continue reading

ByValue, I Think He’s Got It

Recently I responded to an email from a student seeking clarification about the difference between ByValue and ByProperty when it comes to parameter binding. This is what makes pipelined expressions work in Windows PowerShell. When you look at cmdlet help, you’ll see that some parameters accept pipeline binding, which is what you are looking for. Often this means you don’t need to resort to a ForEach-Object construct. Here’s an example.

ByValue means if I see something I’ll use it. ByProperty means I’m looking for a specific object property. For example, Get-Service uses the Name property to find a service.

This parameter accepts both types of binding. This means you can do this:

This is an example of byValue. Get-Service sees something in the pipeline and assumes it is a service name. However, for Get-Service this would also work.

$all[0] is a service object with name property which when piped to Get-Service, finds the property and binds to it. Here’s another example that shows binding by property name can be from any object, not just a service object.

This is a one line expression that leverages the pipeline and uses some helpful (I hope) techniques. The first part using Get-Content retrieves the list of computer names from the text file. Because the text file might have blank lines and some computers might be offline, each name is piped to Where-Object which will only pass on names that exist (skipping blanks) and that can be pinged. To speed things up I’m only sending 2 pings. Now the fun part. I could use ForEach-Object and pass $_ as the value for -Computername. But according to help, this parameter accepts binding by property name.

So I’ll take the computername value coming from Where-Object and use a hash table with Select-Object to define a new “property” name, called Computername. I also take the liberty of trimming off any leading or trailing spaces, just in case. Now I have an object with a property called Computername that is piped to Get-Service which binds on the computername property. The rest is merely formatting.

Look for opportunities to bind by parameter, which means reading cmdlet help which is a good habit to have regardless.

Potential Pipeline Pitfall

Last week I was helping someone out in the PowerShell forum at ScriptingAnswers.com. The specific problem is irrelevant;l however I  learned something in the process that will affect how I write my own PowerShell functions from now on. Not being a developer I never picked up on this subtle (at least to me) distinction. Here’s the deal.

Continue reading

GUI vs CLI

I often talk about using PowerShell GUIs vs the console experience. There is certainly a place for a GUI, but sometimes you need the raw power that comes with a console session. Here’s an example.

Continue reading

Out-Notepad

Maybe this isn’t the most earth shattering PowerShell function you’ll ever come across, but it saves me a few keystrokes. There are times when I want to see the results of  PowerShell expression but the console output is insufficient. I want to see the results in a text file opened in Notepad so I can easily scroll, search or whatever.

Continue reading