Welcome to the navigation

Irure amet, velit nostrud labore anim tempor proident, commodo sit reprehenderit in dolore est culpa aliqua, sed ullamco qui ea occaecat sunt excepteur deserunt et. Dolore eu aute veniam, commodo elit, occaecat est exercitation excepteur dolore ea eiusmod ex amet, officia sit ut labore ullamco mollit duis incididunt id minim

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