Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 用于打印系统路径中的每个值的Powershell脚本_Windows_Scripting_Powershell - Fatal编程技术网

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

Windows 用于打印系统路径中的每个值的Powershell脚本,windows,scripting,powershell,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\So

我发现我可以在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
我宁愿吃这样的东西:

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

适合我。

在Unix上,您只需使用

echo $path | tr ";" "\n"
您可以在Windows上使用以下任一选项模拟此工作流:

或者

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

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

  • 下载tr作为本手册的一部分。把它们提取到别的地方。(例如,“C:\ProgramFiles(x86)\bin\perl\”

  • 将名为tr.bat的bat文件及其内容添加到bin文件夹:

  • @回音

    perl“C:\ProgramFiles(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
    ...
    

    虽然这会起作用,但对于您已经可以在PowerShell中轻松完成的事情来说,这是一种过火的做法。也许吧,但我向那些来自unix环境并且没有兴趣/动机学习PowerShell的人(比如我自己)提供这一点。对不起,这听起来有点屈尊。我的意思是,我现在可以通过“grep Windows”来实现这一点并获取路径中的所有Windows子目录。我不知道如何在Powershell中执行此操作。