POWERSHELL / CHAPTER 08

Literal paths and provider boundaries

Intermediate

Treat a path supplied as data according to the intended lookup semantics.

By the end of this chapter

  • Distinguish wildcard patterns from literal names
  • Use LiteralPath where supported for exact names
  • Check provider-specific behavior before assuming filesystem semantics

The idea

PowerShell paths can identify items in different providers, including filesystem locations and other hierarchical stores. A parameter named Path may support wildcard interpretation, while LiteralPath treats the supplied path as the exact name for cmdlets that offer it. Quoting protects how the shell parses a string, but does not disable wildcard interpretation performed later by the receiving parameter.

This distinction matters for ordinary filenames containing square brackets or wildcard characters. When the caller selects one exact item, preserve that identity with LiteralPath instead of constructing a wildcard expression. When a pattern is intended, document that contract and check the matched set before modifying anything. Avoid assuming that every provider has filesystem properties or that joining path segments validates their existence or permissions.

PowerShell example
$name = 'report[1].txt'
$name -eq 'report[1].txt'
'report1.txt' -like 'report[1].txt'
Result
True
True

Equality treats the brackets as ordinary text. The -like pattern uses [1] as a one-character class, illustrating why a pattern can select a different filename.

Try it yourself

Describe how to inspect an exact bracket-containing filename with Get-Item, using LiteralPath and explicit error handling.

Use your own editor for this exercise. Runnable challenges on CodeQuest are available for JavaScript, Python, and SQL.

CHECK YOUR UNDERSTANDING

Chapter checkpoint

Which Get-Item parameter selects an exact filename that contains square brackets?