PowerShell / PowerShell/PSScriptAnalyzer

Rule request: AvoidUsingBacktickLineTerminator

Open
#2,111 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
2.2k
Forks
414
Avg merge
13h 1m
Merged PRs (30d)
2

Description

Summary of the new feature

As a code reviewer, I want script/module writers to receive automated informational warnings about backtick usage so that my review time isn't consumed by catching maintainability issues that tooling should prevent upfront.

Problem Statement:
The backtick (`) character is commonly used for line continuation in PowerShell, but it's considered poor practice for several reasons:

  • Hard to see: Backticks are nearly invisible and easily missed during code review
  • Poor readability: Makes code harder to read and understand
  • Maintenance issues: Easy to accidentally remove or misplace during editing
  • Non-intuitive: New PowerShell users often struggle with backtick usage

PowerShell offers better alternatives like parameter splatting and natural line breaks after operators/pipelines that are more readable and less error-prone.

Proposed technical implementation details

Rule Name: PSAvoidUsingBacktickLineTerminator

Severity: Information

Behavior:

  • Flag any usage of backtick (`) character used for line continuation
  • Suggest appropriate alternatives based on context

Recommended alternatives to suggest:

  1. Parameter Splatting: For commands with multiple parameters
  2. Natural line breaks: After pipeline operators (|), logical operators (-and, -or), comparison operators
  3. Parentheses grouping: For complex expressions

Example violations:

# Backtick line continuation - Flagged
Get-Process -Name notepad `
    -ErrorAction SilentlyContinue `
    | Where-Object CPU -gt 100

# Complex command with backticks - Flagged  
$result = Get-ChildItem -Path C:\Temp `
    -Filter "*.txt" `
    -Recurse `
    -ErrorAction SilentlyContinue

Technical Implementation:

  • I plan on taking this issue if approved.
  • Simple class that inherits ITokenRule:
public IEnumerable<DiagnosticRecord> AnalyzeTokens(Token[] tokens, string fileName)
{
    if (tokens == null) throw new ArgumentNullException(Strings.NullTokensErrorMessage);

    var lineContinuationTokens = tokens.Where(token => token.Kind == TokenKind.LineContinuation);

    foreach (var tokenNode in lineContinuationTokens)
    {
        yield return new DiagnosticRecord(
            string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingBacktickLineTerminatorError),
            tokenNode.Extent,
            GetName(),
            DiagnosticSeverity.Information,
            fileName
        );
    }
}

What is the latest version of PSScriptAnalyzer at the point of writing

1.24.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining existing ITokenRule implementations and the AnalyzeTokens entry point described in the issue, especially TokenKind.LineContinuation and the diagnostic message in Strings. Confirm how comparable rules are registered and tested. Done means backtick line-continuation tokens produce informational diagnostics with the rule name and source extent, without flagging unrelated tokens.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, powershell
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.