Edit

AvoidUsingDoubleQuotesForConstantString

Severity Level: Information

Description

This rule detects static strings enclosed with double quotes ("<text>") with text that doesn't contain variables, expressions, or special characters that require escaping.

Enclose text with single quotes ('<text>') when the value of a string is constant. A constant string doesn't contain variables or expressions intended to insert values into the string, such as "$PID-$(hostname)". Using single quotes makes the intent clearer that the string is constant and allows you to use special characters such as $ without needing to escape them.

However, there are exceptions where this rule doesn't flag violations:

  • Double-quoted strings are preferred when the string contains single quotes
  • Embedded escape sequences like newline ("`n")
  • Other special characters that require escaping

Example

Noncompliant

$constantValue = "I Love PowerShell"

Compliant

$constantValue = 'I Love PowerShell'