Powershell脚本用于打印系统路径中的每个值

时间:2011-08-22 17:39:08

标签: windows scripting powershell

我发现我可以在PowerShell中使用$env:Path来查看我当前的系统路径。但是,一切都在一条线上运行。有没有办法将$env:Path的输出传递给另一个命令,该命令将分别打印每个路径值(即在新行上打印由分号分隔的所有内容)?

目前打印的内容如下:

C:\Some Test Directory\With\Some\Files\In\It;C:\Some Test Directory\With\Some\Files\In\It;C:\Some Test Directory\With\Some\Files\In\It;C:\Some Test Directory\With\Some\Files\In\It;C:\Some Test Directory\With\Some\Files\In\It;

我宁愿有这样的事情:

C:\Some Test Directory\With\Some\Files\In\It
C:\Some Test Directory\With\Some\Files\In\It
C:\Some Test Directory\With\Some\Files\In\It
C:\Some Test Directory\With\Some\Files\In\It
C:\Some Test Directory\With\Some\Files\In\It

2 个答案:

答案 0 :(得分:13)

$env:Path.split(";")

PS C:\Users\user> $env:Path.split(";")
C:\Program Files (x86)\Haskell\bin
C:\Program Files (x86)\Haskell Platform\2011.2.0.1\lib\extralibs\bin
C:\Program Files (x86)\Haskell Platform\2011.2.0.1\bin
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common
C:\Windows\system32
...

适合我。

答案 1 :(得分:1)

在Unix上,您只需

即可完成此操作
echo $path | tr ";" "\n"

您可以使用以下任一方法在Windows上模拟此工作流程:

EITHER

安装gnu coreutils并将bin目录添加到系统路径

OR

  1. 将命令行工具的文件夹添加到系统路径。例如,“C:\ Program Files(x86)\ bin \”

  2. 下载tr作为powertools ported from unix的一部分。在其他地方提取它们。 (例如,“C:\ Program Files(x86)\ bin \ perl \”

  3. 将名为tr.bat的bat文件与这些内容一起添加到bin文件夹中:

  4.   

    @echo off

         

    perl“C:\ Program Files(x86)\ bin \ perl \ tr”%*

    (路径应该与您提取perl工具的位置匹配)


    结果

    C:\>echo %path% | tr ";" "\n"
    C:\Program Files (x86)\bin\
    C:\Perl\site\bin
    C:\Perl\bin
    C:\WINDOWS\system32
    C:\WINDOWS
    C:\WINDOWS\System32\Wbem
    C:\WINDOWS\System32\WindowsPowerShell\v1.0\
    C:\Python27
    ...