在Windows PowerShell中设置别名


2

在PowerShell中,键入:

PS C:> sal cdp "cd 'C:\Users\ec\Documents\Visual Studio 2010\Projects'"

我没有错,并且

PS C:> gal cdp

将定义显示为:cd'C:\ Users \ ec \ Documents \ Visual Studio 2010 \ Projects'

但是,当我尝试使用cdp时,我得到了:

无法解析别名“ cdp”,因为它引用术语“ cd'C:\ Users \ ec \ Documents \ Visual Studio 2010 \ Projects”,该别名未被识别为cmdlet,函数,可操作程序或脚本文件。验证条款,然后重试。在第1行:char:4
+ cdp <<<<
 + CatergoryInfo:ObjectNotFound(dsp:String)[],CommandNotFoundException
 + FullyQualifiedErrorId:AliasNotResolvedException

我猜这很容易。因此,如果是这种情况,我事先表示歉意。我已经在Google和Google上进行过搜索,并且还阅读了Windows PowerShell Cookbook。

Answers:


4

据我了解,piowershell中的别名的行为不像unix / linux类型的shell中的别名。别名只是键入更长命令的一种简便方法。对于所需的功能,更容易定义一个功能:

函数cdp {设置位置'C:\ Users \ ec \ Documents \ Visual Studio 2010 \ Projects'}

然后,您可以在线执行cdp到达所需位置。或者,您可以为该位置创建一个变量,然后将cd更改为该变量名称:

$cdp = 'C:\Users\ec\Documents\Visual Studio 2010\Projects'
cd $cdp

这是您要达到的目标的两个最简单的解决方案。


谢谢。是的,我正在尝试复制* nix shell脚本行为。您的两个建议都对我有用-因此,我将看看是否可以使它们以某种易于重用的形式保留下来。
westsider

1
即使您尚未就持久性问题提出任何疑问,在powershell提示符下执行get-help about_profile也可以帮助您解决持久性问题
Matrix Mole11

1
谢谢。冒着礼节的危险,我决定使用Documents / WindowsPowerShell /中的profile.ps1作为最简单的路径。这非常适合我的即时需求-就像其他系统上的.profile一样。
westsider

好吧,我也是一个狂热的跑步者。我几乎所有的外壳自定义设置也都进入了AllUsersAllHosts配置文件。
EBGreen
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.