Set-Alias to anonymous script block

Skip to first unread message


Jul 22, 2008, 1:27:02 PM7/22/08
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 '.


Marco Shaw [MVP]

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

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 }


*Microsoft MVP - Windows Server - Admin Frameworks
*PowerShell Co-Community Director -
*Blog -


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

Set-Alias [-name] <string> [-value] <string> [-description <string>]

I wich I was wrong but it seems that the value is restricted to a

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

At first I thought you wanted something useful.
But shortly I began to wonder what the difference between

Set-alias my-fcn {"hi"}
function my-fcn {"hi"}

was supposed to be.


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


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


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


Oct 12, 2017, 4:38:37 PM10/12/17
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 {
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!

Jan 4, 2018, 5:37:23 AM1/4/18
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
0 new messages