0
0
PowerShellscripting~5 mins

String concatenation in PowerShell

Choose your learning style9 modes available
Introduction
String concatenation lets you join two or more pieces of text into one. This helps you build messages or combine data easily.
Creating a full name from first and last names.
Building a file path from folder and file names.
Making a sentence from separate words or phrases.
Combining user input with fixed text for messages.
Syntax
PowerShell
$fullString = $string1 + $string2
Use the plus sign (+) to join strings.
You can also use double quotes with variables inside for easy joining.
Examples
Joins two fixed text pieces into one greeting.
PowerShell
$greeting = "Hello, " + "world!"
Joins first and last name with a space in between.
PowerShell
$firstName = "Jane"
$lastName = "Doe"
$fullName = $firstName + " " + $lastName
Uses double quotes to insert variable directly into the string.
PowerShell
$name = "John"
$message = "Welcome, $name!"
Sample Program
This script joins first and last names with a space and prints the full name.
PowerShell
$firstName = "Alice"
$lastName = "Smith"
$fullName = $firstName + " " + $lastName
Write-Output "Full name is: $fullName"
OutputSuccess
Important Notes
Remember to add spaces manually if you want them between words.
Using double quotes with variables inside is often easier than using + for many parts.
You can join more than two strings by repeating the + operator.
Summary
Use + to join strings simply.
Double quotes allow easy variable insertion.
Add spaces yourself when joining words.