如何创建永久的 PowerShell 别名 [英] How to create permanent PowerShell Aliases

查看:34
本文介绍了如何创建永久的 PowerShell 别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 cmdletalias,它在我关闭 Powershell 的当前会话后不会过期,假设我有这个别名:

I want to create an alias of a cmdlet that doesn't expire after I close the current session of Powershell, let's say I have this alias :

C:\Users\Aymen> New-Alias Goto Set-Location

这完美地创建了 Goto 别名,但我想在关闭当前会话后使用它,我该如何实现.

This perfectly creates the Goto alias, but I want to use it even after I close the current session, how can I achieve that.

注意:

PowerShell 帮助系统建议我可以导出我创建的别名,并在我下次打开新会话时导入它们,实际上这不是我真正想要的,因为有没有一种直接明确的方法来保持通过不同会话创建别名后的别名

The PowerShell Help system suggests that I can export the aliases I create, and import them next time I open a new session, actually that's not really what I'm looking, for, is there a direct clear way to keep having a alias after I create it through different sessions

推荐答案

更新 - 2021 年 1 月

可以在 profile.ps1 文件中存储每次 PowerShell 启动时要执行的任何 PowerShell 代码.至少有 6 种不同的路径 存储代码的位置取决于哪个用户必须执行它.我们将只考虑其中的两个:所有用户"和所有用户".和只有您的用户"路径(点击上一个链接了解更多选项).

It's possible to store in a profile.ps1 file any PowerShell code to be executed each time PowerShell starts. There are at least 6 different paths where to store the code depending on which user has to execute it. We will consider only 2 of them: the "all users" and the "only your user" paths (follow the previous link for further options).

要回答您的问题,您只需创建一个 profile.ps1 文件,其中包含您要执行的代码,即:

To answer your question, you only have to create a profile.ps1 file containing the code you want to be executed, that is:

New-Alias Goto Set-Location

并将其保存在正确的路径中:

and save it in the proper path:

  • $Home\Documents"(通常是 C:\Users\\Documents):只有您的用户才能执行代码.这是推荐的位置您可以通过在 PowerShell 中运行 echo $profile 来快速找到您的个人资料位置
  • $PsHome (C:\Windows\System32\WindowsPowerShell\v1.0):每个用户都会执行这段代码
  • "$Home\Documents" (usually C:\Users\<yourname>\Documents): only your user will execute the code. This is the recommended location You can quickly find your profile location by running echo $profile in PowerShell
  • $PsHome (C:\Windows\System32\WindowsPowerShell\v1.0): every user will execute this code

重要事项:请记住,您需要重新启动 PowerShell 实例才能应用更改.

IMPORTANT: remember you need to restart your PowerShell instances to apply the changes.

提示

  • 如果两个路径都包含一个 profile.ps1 文件,则首先执行所有用户的文件,然后是用户特定的文件.这意味着用户特定的命令将在重复或冲突的情况下覆盖变量.

  • If both paths contain a profile.ps1 file, the all-users one is executed first, then the user-specific one. This means the user-specific commands will overwrite variables in case of duplicates or conflicts.

如果不需要将其执行扩展到每个用户,请始终将代码放在特定于用户的配置文件中.这样更安全,因为您不会污染其他用户的空间(通常,您不想这样做).
另一个优点是您不需要管理员权限即可将文件添加到您的用户空间(您可以在 C:\Windows\System32 中执行任何操作).

Always put the code in the user-specific profile if there is no need to extend its execution to every user. This is safer because you don't pollute other users' space (usually, you don't want to do that).
Another advantage is that you don't need administrator rights to add the file to your user-space (you do for anything in C:\Windows\System32).

如果您确实需要为每个用户执行配置文件代码,请注意 $PsHome 路径对于 PowerShell 的 32 位和 64 位实例是不同的.如果您想总是执行配置文件代码,您应该考虑这两种环境.

If you really need to execute the profile code for every user, mind that the $PsHome path is different for 32bit and 64bit instances of PowerShell. You should consider both environments if you want to always execute the profile code.

路径是:

  • C:\Windows\System32\WindowsPowerShell\v1.0 用于 64 位环境
  • C:\Windows\SysWow64\WindowsPowerShell\v1.0 用于 32 位(是的,我知道,文件夹命名是违反直觉的,但是 正确).
  • C:\Windows\System32\WindowsPowerShell\v1.0 for the 64bit environment
  • C:\Windows\SysWow64\WindowsPowerShell\v1.0 for the 32bit one (Yeah I know, the folder naming is counterintuitive, but it's correct).

这篇关于如何创建永久的 PowerShell 别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆