To create a function, save it as an alias and save it all in your profile later, use:
$g=[guid]::NewGuid(); echo "function G$g { COMMANDS }; New-Alias -Force ALIAS G$g">>$profile
where you replaced ALIAS with the alias you want, and COMMANDS with a command or command line to execute.
Of course, instead you can (and should!) Make an alias for the above:
echo 'function myAlias { $g=[guid]::NewGuid(); $alias = $args[0]; $commands = $args[1] echo "function G$g { $commands }; New-Alias -Force $alias G$g">>$profile }; New-Alias alias myAlias'>>$profile
Just in case, when your brain got inside out from the whole recursion (smoothing aliases, etc.), after inserting the second block of code into PowerShell (and restarting PowerShell), a simple example of its use:
alias myEcho 'echo $args[0]'
or without arguments:
alias myLs 'ls D:\MyFolder'
Iff you don't have a profile yet
The above method will fail if you do not have a profile yet! In this case, use the New-Item -type file -path $profile -force from this answer .
NH. Sep 22 '17 at 20:03 2017-09-22 20:03
source share