Powershell 创建 PHP alias
需要用到 php ,不想在 WSL 里面安装,windows 上是使用的 PHPStudy ,总共装了四个版本的 php ,为了使用不同版本的 php,而不修改文件,决定在 powershell 中设置 alisa 实现。
在 powershell 中,可以使用 Set-Alias GoTo Set-Location 语句来设定 Alias,但是,无法做到持久化。在 linux ,通常通过修改 ~/.bashrc 或 /etc/profile 来实现,终端的启动时配置。
有文如下
Are PowerShell Aliases Saved?
If we close a Windows PowerShell window session, the aliases we have made with the
New-Aliascmdlet are not saved. Therefore, we will need to recreate the same Windows PowerShell aliases if we have aliases set up in our scripts.When we run the script, and the engine fails to map aliases with any cmdlets, it will throw errors. Therefore, we must use
Set-Aliasinstead of theNew-AliasPowerShell cmdlet to prevent these errors from happening. Then, follow the steps below to ensure that Aliases remain active when we open a new Windows PowerShell window or when a PowerShell script runs that use aliases.
- Create a PSConfiguration folder in our Windows PowerShell profile. We can find out our Windows PowerShell Profile by running the
Get-Variable ProfilePowerShell cmdlet.- Inside the PSConfiguration folder, create a file named
Microsoft.PowerShell_Profile.PS1. This file will contain the Windows PowerShell cmdlets to create aliases.- Next, add the PowerShell commands to create aliases in the file.
powershellCopy
Set-Alias Goto Set-Location
但我并不知道文中提到的“PSConfiguration folder” 在哪,
根据 Doc.MicroSoft 提示 :about_Profiles
link: [关于配置文件 - PowerShell | Microsoft Docs]
可以创建 PowerShell 配置文件以自定义环境,并向启动的每个 PowerShell 会话中添加特定于会话的元素。
PowerShell 配置文件是在 PowerShell 启动时运行的脚本。 你可以使用配置文件作为登录脚本来自定义环境。 可以添加命令、别名、函数、变量、管理单元、模块和 PowerShell 驱动器。 你还可以将其他特定于会话的元素添加到你的配置文件中,以便它们可在每个会话中使用,而无需导入或重新创建它们。
PowerShell 控制台支持以下基本配置文件。 配置文件按优先级顺序列出。 第一个配置文件的优先级最高。
说明 路径 所有用户,所有主机 Windows-$PSHOME \Profile.ps1 Linux-/usr/local/microsoft/powershell/7/profile.ps1 macOS-/usr/local/microsoft/powershell/7/profile.ps1 所有用户,当前主机 Windows-$PSHOME \Microsoft.PowerShell_profile.ps1 Linux-/usr/local/microsoft/powershell/7/Microsoft.Powershell_profile.ps1 macOS-/usr/local/microsoft/powershell/7/Microsoft.Powershell_profile.ps1 当前用户,所有主机 Windows $Home \ [My] 文档 \ PowerShell \Profile.ps1 Linux-~/.config/powershell/profile.ps1 macOS-~/.config/powershell/profile.ps1 当前用户、当前主机 Windows $Home \ [My] 文档 \ PowerShell \Microsoft.PowerShell_profile.ps1 Linux-~/.config/powershell/Microsoft.Powershell_profile.ps1 macOS-~/.config/powershell/Microsoft.Powershell_profile.ps1 配置文件路径包含以下变量:
$PSHOME变量,存储 PowerShell 的安装目录$Home变量,用于存储当前用户的主目录
$PROFILE 变量
$PROFILE自动变量存储当前会话中可用的 PowerShell 配置文件的路径。若要查看配置文件路径,请显示变量的值
$PROFILE。 你还可以使用命令中的$PROFILE变量来表示路径。
$PROFILE变量存储 “当前用户,当前主机” 配置文件的路径。 其他配置文件保存在变量的 “注意”$PROFILE属性中。例如,在 Windows PowerShell 控制台中,
$PROFILE变量具有以下值。
说明 名称 当前用户、当前主机 $PROFILE当前用户、当前主机 $PROFILE.CurrentUserCurrentHost当前用户,所有主机 $PROFILE.CurrentUserAllHosts所有用户,当前主机 $PROFILE.AllUsersCurrentHost所有用户,所有主机 $PROFILE.AllUsersAllHosts由于变量的值
$PROFILE会随每个用户和每个主机应用程序更改,因此请确保在使用的每个 PowerShell 主机应用程序中显示配置文件变量的值。若要查看变量的当前值
$PROFILE,请键入:PowerShell 复制
1
2 $PROFILE | Get-Member -Type NoteProperty可以在许多命令中使用该
$PROFILE变量。 例如,下面的命令在记事本中打开 “当前用户、当前主机” 配置文件:PowerShell 复制
1
2 notepad $PROFILE以下命令确定是否已在本地计算机上创建 “所有用户,所有主机” 配置文件:
PowerShell 复制
1
2 Test-Path -Path $PROFILE.AllUsersAllHosts如何创建配置文件
综上所述
我们,只需要打开主机配置文件,编写脚本即可
使用管理员打开终端,使用 notepad $PROFILE.AllUsersAllHosts 命令使用 notepad 打开所有主机所有用户配置文件
如果您只想为当前用户配置,则使用 notepad $PROFILE 即可
打开配置文件后,如下写
1 | Set-Alias php73 ${Env:PHP73}\php.exe |
PHP73 是在系统变量中指定的 PHP7.3 版本的目录,在 powershell 中,使用 $Env 获取系统变量,当然,您可以选择将 php 加入到 $Path 系统变量中,但那样,您就只能使用一个版本的 php ,除非您修改 php.exe 的文件名
此时,您可以通过 php 命令使用 php73 ,而 php5 使用 php5.6
Author: 哒琳
Permalink: http://blog.jieis.cn/2022/940e3662-536a-4cf4-a316-66b53d2e85a1.html
Comments