91

Using the Windows command prompt, can I echo %path% and get the resulting paths on separate rows?

Something like this, but for Windows:

echo $PATH | tr ':' '\n'

Can I do this with vanilla cmd or do I need PowerShell or JavaScript scripting?

Example echo %path% output:

C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;C:\Program Files\Microsoft SQL Server\90\DTS\Binn\;C:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies\;

Desired output:

C:\WINDOWS\system32;
C:\WINDOWS;
C:\WINDOWS\System32\Wbem;
C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;
C:\Program Files\Microsoft SQL Server\90\DTS\Binn\;
C:\Program Files\Microsoft SQL Server\90\Tools\binn\;
C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\;
C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies\;
mrq
  • 460
  • 2
  • 12
Carl R
  • 8,104
  • 5
  • 48
  • 80
  • 1
    Related: [An answer](https://stackoverflow.com/questions/141344/how-to-check-if-a-directory-exists-in-path/8046515#8046515) to *[How to check if a directory exists in %PATH%](https://stackoverflow.com/questions/141344)* deals with all the complications of using simple text parsing for the content of environment variable `path`. – Peter Mortensen Sep 09 '22 at 14:33

3 Answers3

154

Try:

 ($env:Path).Replace(';',"`n")

or

$env:path.split(";")
DimaSan
  • 12,264
  • 11
  • 65
  • 75
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • What is wrong with the following? powershell -Command ($env:Path).Replace(';',"`n") – Carl R Feb 25 '11 at 09:36
  • 4
    PowerShell has `-replace` operator: `$env:Path -replace ';',"`n"` – stej Feb 25 '11 at 11:19
  • 6
    Will fail with quoted paths that contain semicolons. – Joey Feb 25 '11 at 23:27
  • @Joey: What should I do instead then? – Eric Dec 30 '16 at 20:07
  • @Eric: You could use one of the more complicated regexes used for CSV instead, or use a parser. Or hope that no one uses semicolons in paths that appear in `$Env:PATH` since everyone gets it wrong anyway ... – Joey Dec 31 '16 at 11:34
  • @Joey: So there's no native way to split the path in the same way as it is done when resolving executables, without resorting to string manipulation? That's just dumb! – Eric Dec 31 '16 at 13:35
  • @Eric: How frequently do you write scripts that need every item in `$Env:PATH`? I haven't needed that ever so far, and I guess most other people didn't either. – Joey Jan 02 '17 at 19:02
  • 1
    @Joey: I'm more pointing out that internally there must be an implementation of that for `get-command` to work, so exposing that implementation would be easy – Eric Jan 02 '17 at 21:06
  • 1
    @stej Use triple backticks: ```$env:Path -replace ';',"`n"``` – wjandrea Apr 03 '17 at 02:11
46

Fewer keystrokes using either the split operator or method

$env:Path -split ';'
$env:Path.split(';')
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Doug Finke
  • 6,675
  • 1
  • 31
  • 47
11

This works for me (in a cmd window):

powershell -Command ($env:Path).split(';')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
leonardo4it
  • 111
  • 1
  • 6