How to display a nicely formatted path in PowerShell

PowerShell path listing
PowerShell path listing (click to enlarge)

I’m setting up a Chef environment on Windows, and like most devops/architect tools, you need to set the path environment variable to let the system know where all the good stuff is.

(Path is one of those things that never dies.)

Displaying that path is easy if you use the old Windows command prompt shell. (Just enter the command “path“. Duh.)

But we don’t do that anymore, do we? We’re hip — we’ve set up Windows to open PowerShell whenever we open a “command prompt” as shown here:

Replace old command prompt with PowerShell
Replace old command prompt with PowerShell (click to enlarge)

And, guess what, PowerShell has no path cmdlet. So, let’s make our own…one that’s better.

Here’s what I use to display the individual path entries from first to last, each on a separate line:

Push-Location env:
(ls path).value.split(";")
Pop-Location

It may look like a lot of work (three semi-cryptic PowerShell statements) but it demonstrates some very cool PowerShell goodness. Like the concept of “providers” and cool stack-like cmdlets for saving and restoring “locations” in the providers. Even a cool use of the split method.

Note that if you add you scripts directory to the path, you can run this script directly from the shell just by entering its name, in this case pathl. In the image above of my settings, note that my script directory is listed last.

And, if you like, you can add an alias to the profile and add a “path” command to PowerShell.

Set-Alias path -Value pathl.ps1

 


Posted

in

,

by

Tags:

Comments

One response to “How to display a nicely formatted path in PowerShell”

  1. Frank Avatar
    Frank

    An awesome little trick.

Leave a Reply

Your email address will not be published. Required fields are marked *