How To Find Your System Path – Using PowerShell and Environment Variables

Using PowerShell 11 Replies

Just had a couple techs scratching their heads about finding the path.

Seriously? Yes.

But to be fair, the path can get long and downright hard to read.   And that was the problem for our tech friends.

Now my friends are smart guys, but they had a problem with a path.  I can understand, since my path statement is over 800 characters, and nearly impossible to understand when read the normal way.  They should have learned PowerShell.  I’ll show you why, and it’s a great example of how PowerShell blows away other methods by being super easy and ultra flexible.

Everybody knows the normal way to find environment variables:

  • Right clicking “Computer” –> Properties
  • Advanced System Settings –> Advanced Tab –> Environment Variables

If you ever see me get the environment variables this way, you have my permission to punch me in the head.

Note:  If the system I’m working on does not have PowerShell AND does not have Internet access, I’m going to punch you back

How To NOT Find The Path Environment Variable

  1. Search the registry for every instance of the word “PATH”.
  2. Open the computer case and search underneath the CPU fan.
  3. Reading tea leaves and/or chicken bones

There is a better way.  Enter PowerShell

Check out this beauty:

PS C:> cd env:

PS Env:> ls path

And out comes the entry for the path variable

NAME                         VALUE
——                          ——-
Path                          C:\windows\system32;C:\;C:\Sysinternals…

The path environment variable has 2 properties:

KEY
VALUE

You see here that “Name” is actually an alias for “Key”.

PS Env:\> ls path | gm

   TypeName: System.Collections.DictionaryEntry

Name          MemberType    Definition
—-          ———-    ———-
Name          AliasProperty Name = Key

(other methods and noteproperties)

Key           Property      System.Object Key {get;set;}
Value         Property      System.Object Value {get;set;}

If you want to see the actual value (what the path is), try this:

PS Env:> (ls path).value

If you want to see the individual lines in the path:

PS Env:> (ls path).value.split(“;”)

How many characters are in YOUR entire path statement?  Find out!

PS Env:> (ls path).value.length

How many entries are in your path statement?

PS Env:> ((ls path).value.split(“;”) | measure).count

Just list the entries in the path that are in the windows directory and below:

(ls path).value.split(“;”) | ? {$_ -like “C:\Windows*” -or $_ -like “%systemroot%*”}

So now that the horse has been sufficiently beaten, I’ll sign off.

-Michael

11 thoughts on “How To Find Your System Path – Using PowerShell and Environment Variables

  1. Pingback: Tweets that mention How To Find Your System Path – Using PowerShell and Environment Variables | I Love Powershell -- Topsy.com

  2. Pingback: Garden Benches | a Perfect Patio Furniture Addition | Garden Bench Seat

  3. Amy

    [...] H&#959w T&#959 Find Y&#959&#965r System Path – Using PowerShell &#1072&#1495&#1281 Environment &#8… [...]

    Reply
  4. Zi Makki

    Thanks for the blog post… You can also do $env:path to get at the path. All your other scripts will work with that too. IE

    ($env:path).split(“;”)

    hope this helps!

    Zi

    Reply
    1. ILovePowershell Post author

      @dcx

      Sorry for the late reply, new baby Feb 9th and I haven’t been on in a while.

      Remotely, I’d try using WMI

      (gwmi win32_environment -computername REMOTECOMPUTERNAME -credential DOMAIN\UserIfNeededToConnect | ? {$_.name -eq “path” -and $_.username -eq ““}).variableValue.split(“;”)

      Keep the “” the same (it’s the actual name associated with the default path) but change the REMOTECOMPUTERNAME to be the IP or Hostname to be your remote system.

      Credential is only needed if you’re getting access denied.

      Reply

Whats Your Take?