English | 简体中文
PowerShell is the #33 most used language in Github as of 2022 Q1 according to GitHut’s statistics. Let’s take a look at what this scripting language can do.
3 alphanumericals chosen: i
, e
and x
.
$i=$?+$?
$e=$i+$i
$xi=$x=$e+$i
$xe=""+$x+$x
$xx=""+$x+--$e
$x=(""+$?)[$i]
iex "`$ex=""``$x{$xe}"";`$xe=""``$x{$xx}"""
$ii=$e+++$i
$ix=$ii+$i
$ei=$e*$i
$x="``$x{"
iex "`$e=""$x$e$ei}e$x$xi$xe}$x$xi$xe}$x$xi$ex}, $x$ii$ix}$x$xi$ex}$x$ix$i}$x$xi$xe}$x$xi$e}!"""
$e
Semicolons are necessary if the code is written in a single line. The two are equivalent.
$i=$?+$?;$e=$i+$i;$xi=$x=$e+$i;$xe=""+$x+$x;$xx=""+$x+--$e;$x=(""+$?)[$i];iex "`$ex=""``$x{$xe}"";`$xe=""``$x{$xx}""";$ii=$e+++$i;$ix=$ii+$i;$ei=$e*$i;$x="``$x{";iex "`$e=""$x$e$ei}e$x$xi$xe}$x$xi$xe}$x$xi$ex}, $x$ii$ix}$x$xi$ex}$x$ix$i}$x$xi$xe}$x$xi$e}!""";$e
My first thought was if it is possible to directly add or subtract the ASCII value of chars like in most C-style language. However, this is not possible. The target type must be written when it comes to explicit type conversion. [char]65
does give us the character A
, but there are 4 different letters. And if we want to get a number, it’s [int] something
. [1].
Then I try to find the eval function. First I found the very one Invoke-Expression
, but that is too long. Most PowerShell commands are long. That’s bad for a scripting language. Then I found that the ampersand, &
, can execute command in a string. But it works on a single command, without parameters.[2] Luckily, there is an alias of Invoke-Expression
: iex
.
Since there is no way to turn a single char into another, the only way to get a 4th character is to split a String elsewhere. In PowerShell, a character quoted by either ''
or "
is always a string, an array of chars. There is no .pop() in Powershell,[3] so we need to access it by index if we want a specific character.
$i = $? + $?
-
$? contains the execution status of the last command. It is
True
initially or when the last command succeeded.[4] -
The equivalent of
==
is-eq
in Powershell;$i==$i
won’t work. -
True
is1
when casted to an integer.[5] -
Most lines of code are aimed at getting the digits needed. There is still much room for optimization.
$x = ("" + $?)[$i]
-
An empty string plus a boolean turns into a string.
Thanks to the u
in "True"
, we can compose any Unicode character now. The hex for hello world string contains c
and f
. Just get the two first. The $e
variable is actually:
$e = "``u{48}e``u{6c}``u{6c}``u{6f}, ``u{57}``u{6f}``u{72}``u{6c}``u{64}!";
iex "`$ex=""``$x{$xe}"";`$xe=""``$x{$xx}"""
-
To escape
` in front of them or repeat the character itself.[6], `$
,"
, just add
PowerShell prints out the value of the previous expression by default, so there is no need to iex "echo"
something.