Indeed, use of the -LiteralPath
parameter is the best solution (in PowerShell [Core] v6+, you can shorten to -lp
):
$content = Get-Content -LiteralPath $i.Fullname
-LiteralPath
ensures that $i.Fullname
is taken verbatim (literally); that is, [
and ]
in the path are interpreted as themselves rather than having special meaning (see below).
As for what you tried:
$content = Get-Content $i.Fullname
is effectively the same as:
$content = Get-Content -Path $i.Fullname
That is, the (first) positional argument passed to Get-Content
is implicitly bound to the
-Path
parameter.
The -Path
parameter accepts wildcard expressions to allow matching paths by patterns; in addition to support for *
(any run of characters) and ?
(exactly 1 character), [...]
inside a wildcard pattern denotes a character set or range (e.g., [12]
or [0-9]
).
Therefore an actual path that contains [...]
, e.g., foo[10].txt
, is not recognized as such, because the [10]
is interpreted as a character set matching a single character that is either 1
or 0
; that is foo[10].txt
would match foo0.txt
and foo1.txt
, but not a file literally named foo[10].txt
.
When (implicitly) using -Path
, it is possible to escape [
and ]
instances that should be interpreted verbatim, namely via the backtick (`
), but note that this can get tricky to get right when quoting and/or variable references are involved.
If you know a path to be a literal path, it is best to form a habit of using -LiteralPath
(which in PowerShell Core you can shorten to -lp
).
However, if your path contains literal [
and ]
and you also need wildcard matching, you must use `
-escaping - see this answer.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…