本文以为notepad++.exe创建别名为例,说明powershell创建永久别名的步骤。通过为notepad++.exe创建别名,我可以在powershell里的任何地方,通过notepad++别名启动notepad++.exe打开指定文件。
-
找到notepad++.exe安装目录,我的是
"C:\Development Kit\Notepad++\notepad++.exe"
。 -
通过cmdlet创建别名
Set-Alias notepad++ "C:\Development Kit\Notepad++\notepad++.exe"
-
但是这个别名只对当前会话有效,你重新打开一个powershell,这个别名就失效了。如果想让它对每个会话有效,可以把它加入powershell的配置文件
profile
中,profile的脚本会在每次启动powershell时执行。 -
通过
$profile
找到powershell的profile的位置。但是这个文件不一定存在,使用命令Test-Path $profile
进行测试,如果结果为false,证明没有这个文件,需要创建。 -
创建命令如下:
New-Item -path $profile -itemtype file -Force
-
将2中设置别名的命令copy到profile文件中。
-
powershell默认在启动的时候,会以Restricted模式运行,此时不允许执行任何脚本。需执行:
Set-Executionpolicy Remotesigned
去掉严格模式。
-
比如当前目录下我有一个文件test.txt,那么我就可以通过
notepad++ test.txt
,调用notepad++打开这个文件了。