May 11th, 2014

PowerTip: Convert Character Array to String

Doctor Scripto
Scripter

Summary: Easily convert a character array to a string in Windows PowerShell.

Hey, Scripting Guy! Question How can I use Windows PowerShell to convert data that is returning as an array of characters
          instead of as a single string?

Hey, Scripting Guy! Answer Use the unary operator, and pass the array. In the following example, the first command returns an
          array of characters, and the next the unary operator converts the character array to a single string:

PS C:\> $a = (get-module -l)[0].Path | split-path -Parent

PS C:\> $a[0]

C

PS C:\> $b = (,$a)

PS C:\> $b[0]

C:\Users\ed\Documents\WindowsPowerShell\Modules\ConversionModule

PS C:\>

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.

Feedback