Welcome to the navigation

Occaecat do ullamco dolor elit, sit culpa voluptate et commodo quis reprehenderit in anim eu proident, dolore sed mollit nostrud id lorem ea in ex. Fugiat tempor cillum anim dolore laborum, sit dolore esse reprehenderit non nulla dolor in ex veniam, sed ea duis et in aliqua, irure excepteur lorem

Yeah, this will be replaced... But please enjoy the search!

String concatenation in PowerShell

Strings concatenation in PowerShell is an everyday task for almost any Windows IT staff or Microsoft platform developer today. There are a few ways to accomplish this

Our variables

$hello = "Hello"
$world = "World"

The ordinary usage

Using built-in methods in PowerShell

# Concatenated using +
$concatenatedString = $hello + " "  + $world  +  "!"
 
# Using embedded variables
$embeddedString = "$hello $world!"
 
# Using string.Format powershell version (-f for format)
$formattedString = "{0} {1}!" -f $hello, $world

Using native .net methods

# Using string.Concat
$concatenateString = [System.String]::Concat($hello, " ", $world, "!")
 
# Using string.Format
$formattedString = [System.String]::Format("{0} {1}!", $hello, $world)

Preference

That is up to you, performance-wise there is little or no difference when doing a million cycles. My personal preference would be the native .net methods since thats what I'm used to read and I like to keep things simple.

What will happend with line breaks using the different methods

Concatenated using + with line breaks

$concatenatedString = $hello + 
    " "  + 
    $world  +  
    "! "

Returns the entire string in one line

Hello World!

Using embedded variables

$embeddedString = 
    "$hello 
    $world!"

Returns a line broken string

Hello 
    World!

Using string.Format powershell version (-f for format)

$formattedString = "{0} 
    {1}!" -f $hello, $world

Returns a line broken string

Hello 
    World!

Using string.Concat

$concatenateString = 
    [System.String]::Concat($hello, 
        " ", $world, 
        "!")

Returns the entire string in one line

Hello World!

Using string.Format

$formattedString = 
    [System.String]::Format(
        "{0} {1}!", 
        $hello, $world)

Returns the entire string in one line

Hello World!

Using string.Format

$formattedString = 
    [System.String]::Format(
        "{0} 
        {1}!", 
        $hello, $world)

Returns a line broken string

Hello 
        World!
Enjoy
Please note that this portal is a BETA site , I will continuously improve all functionality and performance during the coming weeks / the author