Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What am I allowed to name a function in Powershell?

Tags:

powershell

PS > function ]{1}
PS > ]
1
PS >
PS

Why does this work?

What else can I name a function? All I've found so far that works is * and ].

like image 324
Twilight Sparkle Avatar asked Nov 03 '18 20:11

Twilight Sparkle


1 Answers

You can name it almost anything. You can even include newlines and emoji* in the name.

function Weird`nFunctionの名前😀 { Write-Host hey }
$c = gcm Weird*
$c.Name
& $c

Escaping helps with lots of things like that:

function `{ { Write-Host cool }
`{

function `0 { Write-Host null }
gci function:\?

I'll add that this is true for variables too, and there's a syntax that removes the need to do most escaping in the variable name: ${varname} (as opposed to $varname).

With that, you could easily do:

${My variable has a first name,
it's
V
A
something
R,
whatever I dunno 
🤷} = Get-Process

You'll note that if you then start typing like $MyTAB it will tab complete in a usable way.


To (somewhat) answer why this should work, consider that the variable names themselves are just stored in .Net strings. With that in mind, why should there be a limit on the name?

There will be limits on how some of these names can be used in certain contexts, because the parser will not understand what to do with it if the names don't have certain characters escaped, but literal parsing of PowerShell scripts are not the only way to use functions or variables or other language constructs, as I've shown some examples of.

Being less limiting also means being able to support other languages and cultures by having wide support for character sets.

To this end, here's one more thing that might surprise you: there are many different characters to represent the same or similar things that we take for granted in code, like quotation marks for example.

Some (human) languages or cultures just don't use the same quote characters we do in English, don't even have them on the keyboard. How annoying would it be to type code if you have to keep switching your keyboard layout or use ALT codes to quote strings?

So what I'm getting at here is that PowerShell actually does support many quote characters, for instance, what do you think this might do:

'Hello’

Pretty obvious it's not the "right" set of quotes on the right side. But surprisingly, this works just fine, even though they aren't the same character.

This does have important implications if you're ever generating code from user input and want to avoid sneaky injection attacks.

Imaging you did something like this:

Invoke-Expression "echo '$($userMsg -replace "'","''")'"

Looks like you took care of business, but now imagine if $userMsg contained this:

Hi’; gci c: -recurse|ri -force -whatif;'

For what it's worth, the CodeGeneration class is aware of this stuff ;)

Invoke-Expression "echo '$([System.Management.Automation.Language.CodeGeneration]::EscapeSingleQuotedStringContent($userMsg))'"

* PowerShell Console doesn't have good support for Unicode, even though the language does. Use ISE to better see the characters.

like image 121
briantist Avatar answered Nov 20 '22 07:11

briantist