How to Escape Single Quotes in PowerShell?

If you’re working with PowerShell, you might have come across scenarios to escape single quotes in your scripts. This can be particularly tricky, especially when dealing with strings containing special characters. In this tutorial, I will explain different methods to escape single quotes in PowerShell with examples.

To escape single quotes in PowerShell, the simplest method is to use double-quoted strings. By enclosing your string in double quotes, you can include single quotes without any special handling. For example, if you have a name like O'Connor, you can write it as "$name = "O'Connor" and PowerShell will correctly interpret the single quote within the double-quoted string.

Escape Single Quotes in PowerShell

In PowerShell, strings can be enclosed in single quotes (') or double quotes ("). Escaping single quotes is essential when you need to include them within a single-quoted string. For example, if you’re working with data that includes apostrophes, such as names like “O’Connor” or “D’Angelo,” you need to ensure your script handles these correctly.

Now, let me show you different methods to escape single quotes in PowerShell with examples.

Method 1: Using Double Quotes

One straightforward way to handle single quotes in PowerShell is by using double-quoted strings. This method is simple and often the easiest to implement.

Here is an example.

$name = "O'Connor"
Write-Output "The name is $name"

In this example, the single quote within the name “O’Connor” is handled without any issues because the string is enclosed in double quotes.

Here is the exact output in the screenshot below after I executed the above PowerShell script:

powershell escape single quote

Read PowerShell Variables in Quotes

Method 2: Using the Backtick (`) Escape Character

PowerShell uses the backtick character (`) as its escape character. When you need to include a single quote within a single-quoted string, you can use the backtick to escape it.

Let me show you an example.

$name = 'O`'Connor'
Write-Output "The name is $name"

Here, the backtick before the single quote tells PowerShell to treat the single quote as a literal character.

Method 3: Using Here-Strings

Here-strings are another powerful feature in PowerShell, allowing you to define multi-line strings. They can be enclosed in either single or double quotes, and they preserve the formatting, including line breaks and spaces.

Single-Quoted Here-Strings

$name = @'
O'Connor
'@
Write-Output "The name is $name"

Here is the exact output in the screenshot below:

powershell escape single quote in variable

Double-Quoted Here-Strings

$name = @"
O'Connor
"@
Write-Output "The name is $name"

In both examples, the single quote within “O’Connor” is preserved without any issues.

Check out Add Double Quotes in a String in PowerShell

PowerShell Escape Single Quote in Variable Examples

Let’s look at a few practical examples involving more complex strings to understand it more. Suppose you’re working with a database query or a JSON payload that includes single quotes.

Example 1: Database Query

$query = "SELECT * FROM Users WHERE LastName = 'O`'Connor'"
Invoke-Sqlcmd -Query $query

In this example, the single quote in “O’Connor” is escaped using the backtick, ensuring the query executes correctly.

Example 2: JSON Payload

$jsonPayload = '{
    "user": {
        "name": "O`'Connor",
        "city": "New York"
    }
}'
Invoke-RestMethod -Uri "https://api.example.com/users" -Method Post -Body $jsonPayload

Here, the single quote in the JSON payload is escaped, ensuring the JSON is correctly formatted and can be processed by the API.

Conclusion

In this tutorial, I explained how to escape single quotes in PowerShell using different methods, such as double quotes, Here-Strings, etc. I explained all the methods using various examples.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.