Reverse Strings with PowerShell

Hey, Scripting Guy! I am working with Windows PowerShell, and I need to reverse a string. I am surprised there is no reverse method in the string class. It seems like a major oversight. Anyway, can you show me how to do this easily?

—SS

Hello SS,

Microsoft Scripting Guy, Ed Wilson, is here. This week is an awesome week. The MVP Summit is going on, and the Scripting Wife is in Bellevue/Redmond/Seattle hanging with thousands of MVPs from around the world. She keeps sending me text messages, like, "Dude, I just got to see Jaap," and "I was talking to Mark," and "Guess what? Karl is coming to the get-together tonight." One was, "You are not going to believe it. Vlad invited me to sit in on the Jeremy unplugged session! OMG!!!"

I mean, it is nearly a real-time stream of messages. Then she goes quiet when she is in an NDA session.

Nah, I am not jealous. Nope. Not one bit...

So, SS. I was sitting on the patio overlooking the pool, and I was sipping a nice cup of English Breakfast tea. I have rather poor WiFi, but I turned on Internet Connection Sharing on my Windows 8.1 phone, and boom! My Windows 10 laptop joined the connection and all is good. Actually, the performance is really quite good. I might watch Buffy the Vampire Slayer this evening on Netflix. Yeah, no reason not to. This is working out really well. I digress...

So you have a string. I'll store the string in a variable named $a. I then display the contents of the $a variable. This is shown here:

PS d:\> $a = "abcde"

PS d:\> $a

Abcde

I know that I have a System.String, and I can look this up by using the Get-Member cmdlet. I pipe the $a variable to Get-Member (gm is an alias) and look through the methods and properties associated with the string object:

The first thing I want to do is to break the string into an array of characters—a character array. Luckily, there is a string method that does just that. I will store the character array in a new variable, $b:

PS d:\> $b = $a.ToCharArray()

PS d:\> $b

a

b

c

d

e

So, now I want to reverse the character array. To do this, I use the static reverse method from the [array] object:

PS d:\> [array]::Reverse($b)

PS d:\> $b

e

d

c

b

a

Note The cool thing here is that the array reverses order. I do not need to save the output in a new variable. $b now has a reversed array.

Now I want to regroup the elements of the array back into a string, and store that back into a variable:

PS d:\> $c = -join($b)

PS d:\> $c

Edcba

And my string is back to a string again:

PS d:\> $c.GetType()

IsPublic IsSerial Name BaseType

-------- -------- ---- --------

True True String System.Object

SS, that is all there is to using Windows PowerShell to reverse strings. Join me tomorrow when I will talk about more cools stuff.

I just want to add another solution, that I figured out some time ago and which sticks with Powershell without the Support of .Net functions. Just take a string and let PS do the conversion from string to char Array under cover: