windows Powershell 脚本打印出系统路径中的每个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7151279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:54:55  来源:igfitidea点击:

Powershell script to print out each value in the system path

windowsscriptingpowershell

提问by Ben McCormack

I found that I can use $env:Pathwithin PowerShell to see my current system path. However, everything runs together on one line. Is there a way to pipe the output of $env:Pathto another command that will print each path value separately (i.e. printing on a new line for everything that is delimited by a semi-colon)?

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

Currently it prints something like this:

目前它打印如下内容:

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;

I'd rather have something like this:

我宁愿有这样的事情:

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

回答by Jacob

$env:Path.split(";")

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

Works for me.

对我来说有效。

回答by RJFalconer

On Unix you can do this with simply

在 Unix 上,你可以简单地做到这一点

echo $path | tr ";" "\n"

You can emulate this workflow on Windows with either of the following:

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

EITHER

任何一个

Install gnu coreutilsand add the bin directory to your system path

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

OR

或者

  1. Add a folder for your command line tools to your system path. E.g, "C:\Program Files (x86)\bin\"

  2. Download tr as part of the powertools ported from unix. Extract them somewhere else. (E.g, "C:\Program Files (x86)\bin\perl\"

  3. Add a bat file called tr.bat with these contents to your bin folder:

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

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

  3. 将一个名为 tr.bat 的 bat 文件添加到您的 bin 文件夹中:

@echo off

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

@回声关闭

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

(Path should match where you extracted the perl tools)

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



Result

结果

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
...