设置路径在 Powershell 中使用开源命令
路径
![](/data/attachment/album/202211/01/160141i03e33e8pp5xp3vs.jpg)



> 
> 在 Windows 上设置你的路径，这样你就可以使用开源的命令。
> 
> 
> 


当你在操作系统上启动应用程序时，操作系统需要使用某些代码库和实用程序来运行该应用程序。你的操作系统知道如何找到这些库和实用程序，因为它有一个 *系统路径*，这是一个通往许多应用程序需要的共同共享数据的地图。所有操作系统都有这一点，但用户通常不会意识到这一点，因为他们通常不需要在意它。然而，当你需要编程或使用特殊的网络实用程序或命令时，你可能需要关心你自己的 `PATH` 变量配置。


`PATH` 变量使你可以将命令保存到一致的位置，并使用命令提示符或更强大（而开源的）[Powershell](https://opensource.com/article/18/2/powershell-people) 从系统上的任何位置调用它们。


例如，假设你想安装开源应用程序 `pscp.exe`，它是 Windows 上著名的 PuTTY OpenSSH 客户端的命令行界面。你可以将它下载到你的硬盘，但是你的命令行如何知道它的存在呢？其实一开始，它并不知道：



```
PS> pscp
 pscp: 命令 “pscp” 不能被识别为 cmdlet、脚本文件或可操作程序的名称。
 检查名称的拼写，或者如果包含了路径，则检查路径是否正确，然后再试一次。

```

如果你正在使用一个开源命令行，例如 Powershell 或 [Cmder](http://cmder.app/)，那么你将得到一个有用的错误提示，提示这可能是你的路径有问题（或缺少路径）。下面是解决这个问题的方法。


### 设置 PATH


首先，在桌面上创建一个名为 `App` 的文件夹。


接下来，右键单击屏幕左下角的 Windows 菜单，然后选择 “<ruby> 系统 <rt>  System </rt></ruby>”。


![Image of the Windows menu system.](/data/attachment/album/202211/01/160244rlkp49lql18sgq9s.jpg)


在弹出的 “<ruby> 系统 <rt>  System </rt></ruby>” 窗口中，单击窗口左侧的 “<ruby> 高级系统设置 <rt>  Advanced system settings </rt></ruby>” 链接。


在出现的 “<ruby> 系统属性 <rt>  System properties </rt></ruby>” 窗口中，单击窗口底部的 “<ruby> 环境变量 <rt>  Environment variables </rt></ruby>” 按钮。


![Image Windows system enviroment variables.](/data/attachment/album/202211/01/160250idgpgpg2i5upzpke.jpg)


在 “<ruby> 环境变量 <rt>  Environment variables </rt></ruby>” 窗口中，单击 “<ruby> 用户变量 <rt>  User variables </rt></ruby>” 面板下的 “<ruby> 新建 <rt>  New </rt></ruby>” 按钮。


![Image of new Windows enviroment variables.](/data/attachment/album/202211/01/160300oibjeibbuxz8u7ph.jpg)


在弹出的对话框中，为 “<ruby> 变量名 <rt>  Variable name </rt></ruby>” 字段输入 `PATH`，为 “<ruby> 变量值 <rt>  Variable value </rt></ruby>” 字段输入 `%USERPROFILE\Desktop\App` 。单击 “<ruby> 确定 <rt>  OK </rt></ruby>” 按钮保存更改。


![Image of Windows path set.](/data/attachment/album/202211/01/160308x09fli3j2i8231dl.jpg)


在 `Desktop/Apps` 中放置你想从命令提示符中访问的命令和应用程序，Powershell、Cmder 甚至 Cmd 都能找到它们。



```
PS> pscp –version
 pscp: Release 0.XY
 Build platform: 64-bit x86 Windows
 PS>

```

### 自动设置路径


许多应用程序会在安装过程中自动添加到系统路径中。然而，并不是所有的程序都如此，要么是因为你在安装过程中遗漏了一个复选框，要么是因为应用程序开发人员希望你自己添加它。当自动路径失败时，你现在知道如何自己设置路径。


*（图像来自：Alan Smithee, CC BY-SA 4.0）*




---


via: <https://opensource.com/article/22/10/set-path-powershell>


作者：[Alan Smithee](https://opensource.com/users/alansmithee) 选题：[lkxed](https://github.com/lkxed) 译者：[qfzy1222](https://github.com/qfzy1233) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
When you launch an application on an operating system, there are certain code libraries and utility applications that your OS needs to use for that app to run. Your OS knows how to find these libraries and utilities because it has a *system path,* a map to common shared data that lots of apps need. Every OS has this, but users aren’t usually aware of it because they don’t usually need to care about it. However, when you start coding or using special network utilities or commands, you might care about your own PATH variable.


The PATH variable makes it so that you can save commands to a consistent location, and use them from anywhere on your system using the command prompt or the more powerful (and open source) [Powershell](https://opensource.com/article/18/2/powershell-people).

For instance, say you want to install the open source application `pscp.exe`

, a command-line interface to the famous PuTTY OpenSSH client on Windows. You can download it to your hard drive, but how does your command-line know that it exists? Well at first, it doesn’t:


```
``````
PS> pscp
pscp: The term 'pscp' is not recognized as the name of a cmdlet, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
```


If you’re using an open source command line, such as Powershell or [Cmder](http://cmder.app/), you get a useful error hinting that this might be a problem with your path (or the lack thereof). Here’s how to solve that problem.

## Setting a PATH

-
First, create a folder called

`App`

on your Desktop. -
Next, right-click on the Windows menu in the bottom left corner of your screen, and select

**System**.

![Image of the Windows menu system.](https://opensource.com/sites/default/files/2022-10/windows-menu-system.png)

(Alan Smithee, CC BY-SA 4.0)

-
In the

**System**window that appears, click the link to**Advanced system settings**on the left of the window. -
In the

**System properties**window that appears, click the**Environment variables**button at the bottom of the window.

![Image Windows system enviroment variables.](https://opensource.com/sites/default/files/2022-10/windows-system-environment-variables.png)

(Alan Smithee, CC BY-SA 4.0)

- In the
**Environment variables**window, click the**New**button under the**User variables**panel.

![Image of new Windows enviroment variables.](https://opensource.com/sites/default/files/2022-10/windows-environment-variables-new.png)

(Alan Smithee, CC BY-SA 4.0)

- In the dialog box that appears, enter
`PATH`

for the**Variable name**field, and`%USERPROFILE\Desktop\App`

for the**Variable value**field. Click the**OK**button to save your changes.

![Image of Windows path set.](https://opensource.com/sites/default/files/2022-10/windows-path-set.png)

(Alan Smithee, CC BY-SA 4.0)

Place commands and applications you want to have access to from a command prompt in `Desktop\Apps`

and Powershell, Cmder, and even Cmd will find them:


```
``````
PS> pscp –version
pscp: Release 0.XY
Build platform: 64-bit x86 Windows
PS>
```


## Automatic PATH settings

Many applications get automatically added to the system path during installation. However, not all of them do, either because you missed a check box during the install process, or because the application developer expects you to add it yourself. When automatic paths fail, you now know how to forge your own path.

## 3 Comments