PowerShell Array String Conversion

How PowerShell Converts an Array into a String

The problem is getting the formatting right when you convert chunks of data into a single string.  The solution is an operator called -Join.

 ♦

PowerShell String Problem

Scenario, you want to create a single string from data stored in assorted database fields.

Problem 1) Each field on a separate line

# Mission: To use PowerShell to join data
$First = "Alan"
$Middle = "Guy"
$Last = "Thomas"
$First, $Middle, $Last

#Result: an unwanted list output:
Alan
Guy
Thomas

Problem 2) DataRunsTogther

# PowerShell to join data: Problem
$First = "Alan"
$Middle = "Guy"
$Last = "Thomas"
"Here is your bonus " +$First +$Middle +$Last

#Result: the data runs together
AlanGuyThomas

Solution to Array String Problem: -Join ‘ ‘

The -Join operator allows us to place a delimiter between the elements, in this instance the space – ‘ ‘ – makes sense because this what we normally place between words.

# Employ -Join to concatenate data strings.
Clear-Host
$First = "Alan"
$Middle = "Guy"
$Last = "Thomas"
$First, $Middle, $Last -Join ‘ ‘

Note:  Observe the comma (,) between each field.

Note: The rhythm of the command is <String>, -Join, finally the delimiter.

Result: Success! We have combined 3 strings to make one string element.

#Result
Alan Guy Thomas

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Further Information About PowerShell’s -Join Operator

PowerShell supports a comprehensive series of ‘about’ documents; -Join is an operator, the opposite of -Split see more thus:

# Research About PowerShell -Join
Clear-Host
Get-Help about_Join

Incidentally, PowerShell has a whole family of ‘about_xyz’ topics.

Controlling PowerShell Arrays with the $OFS Variable

$OFS is a special PowerShell variable, it’s full name says it all: Output Format Separator.

It works well with arrays if you prefer a different separator from a single space.  Here is an example of converting an array to a string, with a comma separator:

Clear-Host
$First = "Alan"
$Middle = "Guy"
$Last = "Thomas"
$Age = "63"
$Person = $First, $Middle, $Last, ($Age/2 -as[int])
$OFS = ‘, ‘
[String]$Person
$OFS = $nul
[String]$Person

#Result showing the effect of $OFS
Alan, Guy, Thomas, 32
Alan Guy Thomas 32

See more about Arrays in PowerShell »

Summary of PowerShell’s Array String Conversion

PowerShell supports the -Join operator, which is ideal for combining multiple strings into a single element, for example FirstName, Lastname – Full Name.

If you like this page then please share it with your friends

 


See more PowerShell examples for syntax advice

PowerShell Syntax  • PowerShell Array  • PowerShell Array String Conversion  • Plist

Get-Date  • PowerShell Quotes  • PowerShell variables  • RegEx  • PowerShell functions

• PowerShell Tutorials  • Conditional operators  • PowerShell Hashtable   • Windows PowerShell

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.