Table of Contents
Using Backtick Characters
Use backtick characters to add double quotes to string in PowerShell.
1 2 3 4 |
$string = "Hello `"World`"" $string |
1 2 3 |
Hello "World" |
Backtick character is escape character in PowerShell. Here, it is used to escape double quotes in the String.
Use backtick characters to add single quotes to string in PowerShell.
1 2 3 4 |
$string = "Hello `'World`'" $string |
1 2 3 |
Hello 'World' |
Using String Concatenation Operator
Use string concatenation operator (+
) to add double quotes to string in PowerShell.
1 2 3 4 |
$string = "Hello " + """World""" $string |
1 2 3 |
Hello "World" |
Use string concatenation operator (+
) to add single quotes to string in PowerShell.
1 2 3 4 |
$string = "Hello " + "'World'" $string |
1 2 3 |
Hello 'World' |
Using Format Operator
Use format operator to add double quotes to string in PowerShell.
1 2 3 4 5 |
$name = "John Williamson" $string = "Hi! This is ""{0}""." -f $name $string |
1 2 3 |
Hi! This is "John Williamson". |
Use format operator to add single quotes to string in PowerShell.
1 2 3 4 5 |
$name = "John Williamson" $string = "Hi! This is '{0}'." -f $name $string |
1 2 3 |
Hi! This is 'John Williamson'. |
We used the format operator (-f
) to format strings by inserting values into the placeholders within the string. This operator works using the format string, which contains placeholders indicated by curly braces ({}
). These placeholders can have format specifiers illustrating how the value must be formatted.
Now, the point is which value will be inserted into the placeholders? For that, the -f
operator is used. The values to be inserted into the specified placeholders are mentioned as arguments to the -f
operator. In the above example, {0}
is the placeholder while $name
is the argument to the -f
operator. Don’t forget to enclose the {0}
within quotes; refer to the above examples to find out how to have single or double quotes.
Using Replace()
Method
Use the Replace()
method to add double quotes to the string in PowerShell.
1 2 3 4 5 |
$string = "Hello World" $string = $string.Replace("World", """World""") $string |
1 2 3 |
Hello "World" |
Use the Replace()
method to add single quotes to a string in PowerShell.
1 2 3 4 5 |
$string = "Hello World" $string = $string.Replace("World", "'World'") $string |
1 2 3 |
Hello 'World' |
The Replace()
method took two arguments. The first is the string we want to replace, and the second is the replacement. Don’t forget that Replace()
does a case-sensitive replacement.
Further reading:
Using -replace
Operator
Use the -replace
operator to add double quotes to the string in PowerShell.
1 2 3 4 5 |
$string = "Hello World" $string = $string -replace "World", """World""" $string |
1 2 3 |
Hello "World" |
Use the -replace
operator to add single quotes to a string in PowerShell.
1 2 3 4 5 |
$string = "Hello World" $string = $string -replace "World", "'World'" $string |
1 2 3 |
Hello 'World' |
The -replace
method is similar to the Replace()
method, replacing the old string with a new string, but it does case-insensitive replacement, which is the same as the -ireplace
operator does. Use the Replace()
method or the -creplace
operator for case-sensitive replacement.
Using New-Object
Cmdlet
Use the New-Object
cmdlet to create a string object having double quotes in PowerShell.
1 2 3 4 |
$string = New-Object String("Hello `"World`"") $string |
1 2 3 |
Hello "World" |
Use the New-Object
cmdlet to create a string object having single quotes in PowerShell.
1 2 3 4 |
$string = New-Object String("Hello`'World`'") $string |
1 2 3 |
Hello 'World' |
We used the New-Object
cmdlet to create an instance of the String
class. Then, the String()
constructor was used to initialize the $string
with a value containing the quotes, whether double or single.
Using ForEach
Cmdlet to add double quotes to each element of array
Use the ForEach
cmdlet to add double quotes to each array element where each element is a string value.
1 2 3 4 5 |
$stationary = @("pencil", "notebook", "paper") $quotedStationary = $stationary | ForEach-Object { """$_""" } $quotedStationary |
1 2 3 4 5 |
"pencil" "notebook" "paper" |
Use the ForEach
cmdlet to add single quotes to each array element where each element is a string value.
1 2 3 4 5 |
$stationary = @("pencil", "notebook", "paper") $quotedStationary = $stationary | ForEach-Object { "'$_'"} $quotedStationary |
1 2 3 4 5 |
'pencil' 'notebook' 'paper' |
In the above examples, we used the array operator (@()
) to create an array having string values and stored it in the $stationary
variable. Then, we piped $stationary
to the ForEach-Object
cmdlet to iterate over $stationary
. In each iteration, we enclosed the current element, represented by $_
, within quotes.
That’s all about PowerShell add quotes to String.