Post by ScottFair question, perhaps it would help if I explain my motiviation.
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
Post by RickBPost by ScottI would like to set an alias to the value of an anonymous script block.
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!
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
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.- Hide quoted text -
- Show quoted text -
Just back from vacation or I'd have answered earlier.
This is probably the basis of how I'd handle it in a loop.