Set-Alias to anonymous script block

654 views
Skip to first unread message

Scott

unread,
Jul 22, 2008, 1:27:02 PM7/22/08
to
I 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!

Marco Shaw [MVP]

unread,
Jul 22, 2008, 1:35:39 PM7/22/08
to

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

RickB

unread,
Jul 22, 2008, 4:31:03 PM7/22/08
to

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.

Scott

unread,
Jul 23, 2008, 12:02:03 PM7/23/08
to
Fair 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

RickB

unread,
Jul 28, 2008, 9:55:51 AM7/28/08
to
> > 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.

$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

unread,
Aug 14, 2008, 4:53:01 PM8/14/08
to
That's perfect! Thanks for your help.

Scott

boers...@gmail.com

unread,
Oct 12, 2017, 4:38:37 PM10/12/17
to
Hey all,
I saw this post when searching for a solution for the same problem.
I've created a small wrapper function. Now I can create my aliases simply by typing "da <alias> '<cmd>'.

So even if this is an old thread maybe there is someone who is interested in the code. Here you go:

function Set-DynamicAlias {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,Position=0)]
[String]$Alias,
[Parameter(Mandatory=$true,Position=1)]
[String]$Command
)
New-Item -Path function:\ -Name "global:$Alias" -Value "$Command `$args" -Force | Out-Null
}
New-Alias -Name da -Value Set-DynamicAlias -Force -Option AllScope

da s1 'Write-Host "sample 1"'

Have a nice day!
Felix

janice6...@gmail.com

unread,
Jan 4, 2018, 5:37:23 AM1/4/18
to
I don't understand any of this.
It is impossible for me to do
anything online.
There was three IP addresses on facebook.
They were not mine.
I traced all of them and they are all blacklisted.

Every account I have has been hacked.
So I have no email to send messages.
Everything I try to do I get the same answer
No Permissions, your admin has blocked them.

I have tried to send notes to 'My Documents'
No one answers.
My images are used as 'tools'

I have no secure browsers.
I can't find any of my files.

I don't care about an alias as I do nothing wrong.
Reply all
Reply to author
Forward
0 new messages