Converting VBScript's StrReverse Function

Definition: Returns a string in which the character order of a specified string is reversed.

StrReverse

How many times have you said to yourself, “Gosh, if only there was a way for me to reverse the character order of a specified string”? That’s what we thought.

Shockingly, Windows PowerShell has no equivalent to VBScript’s StrReverse function; fortunately, though, we found at least one way to achieve the same effect. The following set of commands (which we won’t bother explaining in any detail) assigns the value Scripting Guys to the variable $a, then uses a For Next loop to reverse the order of the characters in the string:

$a = "Scripting Guys"
for ($i = $a.length - 1; $i -ge 0; $i--) {$b = $b + ($a.substring($i,1))}

When you run this command and then echo back the value of $b you should get the following:

syuG gnitpircS

Note. Believe it or not, that’s what you’re supposed to get back!

Return to the VBScript to Windows PowerShell home page