set-alias code & { param() cd C:\Code }
Ampersand not allowed. The & operator is reserved for future use; use "&" to
pass ampersand as a string.
set-alias code { param() cd C:\Code }
Set-Alias : A parameter cannot be found that matches parameter name 'cd
C:\Code '.
Thanks!
v2 CTP2:
PS>set-alias code { param() cd C:\Code }
Set-Alias : Cannot evaluate parameter 'Value' because its argument is
specified as a script block and there is no input
. A script block cannot be evaluated without input.
At line:1 char:10
+ set-alias <<<< code { param() cd C:\Code }
PS>
Marco
--
*Microsoft MVP - Windows Server - Admin Frameworks
https://mvp.support.microsoft.com/profile/Marco.Shaw
*PowerShell Co-Community Director - http://www.powershellcommunity.org
*Blog - http://marcoshaw.blogspot.com
Set-Alias [-name] <string> [-value] <string> [-description <string>]
I wich I was wrong but it seems that the value is restricted to a
string.
PS 205> set-alias tmp -value '{"try this"}'
PS 206> tmp
Cannot resolve alias 'tmp' because it refers to term '{"try this"}',
which is not recognized as a cmdlet, function, operable program, or
script file. Verify the term and try again.
At line:1 char:4
+ tmp <<<<
It appears as though the value of the alias is being executed
in a maner vaguely similar to this:
invoke-expression "${$alias_value} @args"
That is, the value of the alias must be the name of something that
will take arguments rather than just 'something' that will take
arguments.
At first I thought you wanted something useful.
But shortly I began to wonder what the difference between
Set-alias my-fcn {"hi"}
and
function my-fcn {"hi"}
was supposed to be.
When using powershell I want to be able to quickly navigate directories.
I want to type 'myAlias' and move to C:\This\Is\aLong\path\to\type
In other shells I have accomplished this by setting up aliases, so I'm
trying to do the same in powershell.
My approach is to modify the Microsoft.PowerShell_profile.ps1 file like so
function GoToCode()
{
cd C:\Code
}
set-alias code GoToCode
This works but it gets a little verbose when defining a lot of aliases. I
thought it
would be nice to condense to one line like so
set-alias code { param() cd C:\Code }
Then in the future I could get clever and have an array of key/value pairs
which I could loop through and set up the aliases programmatically
It wouldn't surprise me if there is an easier way to do this :)
Any ideas?
Thanks
Just back from vacation or I'd have answered earlier.
This is probably the basis of how I'd handle it in a loop.
$cmd = 'code'
$path = 'c:\code'
new-item -path function: -name $cmd -value "cd $path"
At the moment I just have a few of these:
ni -p function: -n v1 -va 'cd (split-path $profile)'
ni -p function: -n v2 -va 'cd ..\macros'
Scott