87

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 js 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\;
Carl R
  • 7,974
  • 5
  • 45
  • 78

3 Answers3

150

Try:

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

or

$env:path.split(";")
DimaSan
  • 11,496
  • 11
  • 63
  • 74
Ekkehard.Horner
  • 37,907
  • 2
  • 42
  • 88
  • 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
  • 23,210
  • 7
  • 49
  • 68
Doug Finke
  • 6,522
  • 1
  • 29
  • 46
11

this works for me (in a cmd window):

powershell -Command ($env:Path).split(';')
leonardo4it
  • 111
  • 1
  • 6