Discussion:
Set-Alias to anonymous script block
(too old to reply)
Scott
2008-07-22 17:27:02 UTC
Permalink
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]
2008-07-22 17:35:39 UTC
Permalink
Post by Scott
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!
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
2008-07-22 20:31:03 UTC
Permalink
Post by Scott
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!
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
2008-07-23 16:02:03 UTC
Permalink
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
Post by RickB
Post by Scott
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!
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.
RickB
2008-07-28 13:55:51 UTC
Permalink
Post by Scott
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
Post by RickB
Post by Scott
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!
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.

$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
2008-08-14 20:53:01 UTC
Permalink
That's perfect! Thanks for your help.

Scott
Post by RickB
Post by Scott
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
Post by RickB
Post by Scott
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!
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.
$cmd = 'code'
$path = 'c:\code'
new-item -path function: -name $cmd -value "cd $path"
ni -p function: -n v1 -va 'cd (split-path $profile)'
ni -p function: -n v2 -va 'cd ..\macros'
b***@gmail.com
2017-10-12 20:38:36 UTC
Permalink
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
Post by RickB
Post by Scott
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
Post by RickB
Post by Scott
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!
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.
$cmd = 'code'
$path = 'c:\code'
new-item -path function: -name $cmd -value "cd $path"
ni -p function: -n v1 -va 'cd (split-path $profile)'
ni -p function: -n v2 -va 'cd ..\macros'
j***@gmail.com
2018-01-04 10:37:21 UTC
Permalink
Post by Scott
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!
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.

Continue reading on narkive:
Loading...