如何在PowerShell中列出特定目录的内容?
我对Powershell一般感到困惑。很奇怪。无论如何,我已经阅读了:
PowerShell等于Linux's:ls -al -al
,所以我现在知道如何列出列出的内容。当前目录。但是如何列出任意目录的内容?
具体来说,
-
如何输入要检查的路径?
- 在Windows中,
\
是目录分隔符;但这也是大多数语言中的逃生炭。我该怎么办? - 我需要单价吗?双重报价?无报价?
- 在Windows中,
-
我将参数相对于交换机放在哪里?之后,就像我曾经或以前一样?
-
如果我想使用环境变量或powershell变量作为列表路径的一部分 - 我该怎么做?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般powershell信息和示例:
powershell本地命令,包括选择加入的用户作者,具有标准化的参数语法和绑定规则,因此以下内容专注于
get-childitem
,通常适用:请参阅代码> get-childitem ,描述了命令的目的,语法和各个参数,并显示示例。
至于如何读取
语法
cmdlet help help help topics下列出的支持参数的符号,powerShell调用 syntax图< /em>,请参阅概念帮助主题。离线,您可以使用标准交换机
??
或将命令名称传递到get-command -syntax
() > get-childitem-?
或get-command -syntax get-childitem
)。要访问Help text 离线,您可能必须先安装本地帮助,如上所述。示例:
回答您的特定问题,对于来自posix兼容壳的读者,例如bash :
提供两种指定一个或多个输入路径的方法,因为PowerShell中的大多数文件处理CMDET都可以:
-path
接受一个或多个被解释为aget-childitem -path*.txt
,get-childitem“*.txt”
和get-childitem'*.txt'.txt'
都是等效的;请参阅下面的更多信息(请在后两个呼叫中注意path
的偶然省略,这使“*。txt”
and '*。代码>绑定位置与第一个位置参数,-path
)。-literalPath
接受一个或多个名称或路径,这些名称或路径被认为是参考现有文件系统项目字面意思(verbatim)。鉴于unix样平台上的文字路径通常 包含
*
和?
?字符以及Windows 不能,使用-literalpath
进行歧义,通常仅对于包含[
>的路径(可能也是]
),给定那支持字符集和范围(例如[ab]
和[0-9]
)。通过参数绑定到
-literalPath
需要明确使用-literalAlath Pather
,即使用a 的使用/em>参数;例如Get -Childitem -literalPath foo
但是,提供
system.io.fileinfo
和/或system.io.directoryinfo
instances(例如(例如(另一个)get-输出) childItem
或 呼叫)通过管道确实绑定到-literalPath
,如此答案。在powershell
\
是不是的逃脱字符,因此\
实例被视为文字,并且do not not 需要逃脱;它是`
,所谓的 backtick 在powerShell中用作逃生字符 - 请参阅概念 about_special_characters 帮助主题。但是,请注意,PowerShell通常允许您在路径互换中使用
\
/ C:/Windows 正常工作。注意:
所有参数具有 name 在powershell 中 - 也就在 options 之间LS -L/)。
get-path/
作为get-path-path-path/
的速记)。只有需要一个值的参数(参数) can 可能仅以 value仅通过 value - 取决于目标命令的参数声明 - 情况订单重要:
仅值参数称为 positional 参数,它们以的方式绑定对于 声明为位置的那些参数,如果有名,请参见此答案对于如何发现给定命令的哪个参数是位置的。
以其目标参数(例如,
-literalpath/some/path
)将值前缀为 naty 参数。powershell中的A switch (flag),例如
-force
,是一种特殊的参数类型 - 自然界中的布尔值 - 根据定义,它要求将其作为a 命名 /em>参数,通常没有值(尽管您 can 附加一个值,例如force:$ true
或-force -force :$ false
- 请注意,<代码>:是需要将参数名称与其值分开; >此答案有关详细信息)。由于PowerShell允许命名参数以任何 顺序指定,因此这意味着您可以自由放置诸如
force之类的副定义的开关参数
命令行上的任何位置。简而言之:顺序仅在powershell中位置(未命名)论点。
请参阅概念
路径(通常是参数值)需要引用:
如果它包含powershell metacharacters,则尤其是 spaces ;例如
c:\ path \ to \ foo
不需要报价,而c:\ with spaces \ to \ fo \ foo
dive。如果它以 subexpression(
$(...)
),在这种情况下,您需要 double quoting,即“ ...”
(见下文) - 尽管您可以选择始终使用“ ...”
- 引用涉及子表达或变量参考的路径。请注意, powershell具有 no 概念,与posix兼容的壳体中所谓的壳膨胀等效等效,例如bash;因此,是否引用给定参数会产生语义差异(假设它不需要需要引用);如上所述,
*。txt
,'*。txt'
和“*。txt”
都是等效的,它是 target命令而不是powerShell本身,可以解释模式 - 请参见此答案有关更多信息。 P>如果需要引用:
verbatim(单引号)字符串(
'...'...'
)处理他们的内容 verbatimExpandable(double-quoted) >) persion 字符串插值(“展开”嵌入式变量和子表达,即用它们的值替换它们)。
要使用这样的变量,不需要引用(即使值包含空格):
要使较大的字符串的变量(或表达式)部分嵌入);例如:
注意:
嵌入来自表达式或命令的输出需要使用
$(...)
,get-childitem“ $(get-variable -valueonly home)/桌面”
;有关PowerShell可扩展字符串(字符串插值)的完整概述,请参见此答案。。从上面的示例中,省略
“ ...”
引用也可以工作 - 请参见此答案有关更多信息。此外,PowerShell允许您使用
(...)
,分组操作员将任意表达式和命令作为参数的结果;以上是上面命令的等效:或者,使用 JOIN-PATH cmdlet:
General PowerShell information and examples:
PowerShell-native commands, including user-authored ones that opt in, have standardized parameter syntax and binding rules, so the following, focused on
Get-ChildItem
, applies generally:See the help topic for
Get-ChildItem
, which describes the command's purpose, syntax, and individual parameters, along with showing examples.Update-Help
in PowerShell (Core) 7+), you can also print the examples withGet-Help -Example Get-ChildItem | more
As for how to generally read the notation describing the supported parameters listed under the
SYNTAX
heading of cmdlet help topics, which PowerShell calls syntax diagrams, see the conceptual about_Command_Syntax help topic.Offline, you can print the syntax diagrams for PowerShell-native commands with standard switch
-?
or by passing the command name toGet-Command -Syntax
(Get-ChildItem -?
orGet-Command -Syntax Get-ChildItem
). To also access the help text offline, you may have to install local help first, as described above.Examples:
To answer your specific questions, for readers who come from POSIX-compatible shells such as Bash:
Get-ChildItem
offers two ways to specify one or more input paths, as most file-processing cmdlets in PowerShell do:-Path
accepts one or more names or paths that are interpreted as a wildcard pattern.Get-ChildItem -Path *.txt
,Get-ChildItem "*.txt"
, andGet-ChildItem '*.txt'
are all equivalent; more on that below (note the incidental omission of-Path
in the latter two calls, which makes"*.txt"
and'*.txt'
bind positionally to the first positional parameter,-Path
).-LiteralPath
accepts one or more names or paths that are assumed to refer to existing file-system items literally (verbatim).Given that literal paths on Unix-like platforms usually do not contain
*
and?
characters and on Windows cannot, use of-LiteralPath
for disambiguation is usually only necessary for paths that contain[
(and possibly also]
), given that PowerShell's wildcard pattern language also supports character sets and ranges (e.g.[ab]
and[0-9]
).Binding to
-LiteralPath
via an argument requires explicit use of-LiteralPath
, i.e. use of a named argument; e.g.,Get-ChildItem -LiteralPath foo
However, supplying
System.IO.FileInfo
and/orSystem.IO.DirectoryInfo
instances (such as output by (another)Get-ChildItem
orGet-Item
call) via the pipeline DOES bind to-LiteralPath
, as explained in this answer.In PowerShell
\
is not an escape character, so\
instances are treated as literals and do not require escaping; it is`
, the so-called backtick that serves as the escape character in PowerShell - see the conceptual about_Special_Characters help topic.Note, however, that PowerShell generally allows you to use
\
and/
in paths interchangeably, so that, e.g.,Get-ChildItem C:/Windows
works just fine.Note:
All parameters have names in PowerShell - that is, there is no POSIX-like distinction between options (e.g.
-l
and operands (value-only arguments, such as the/
inls -l /
).A command may declare parameters that may also be passed by value only, positionally, meaning that prefixing the value with the parameter name is then optional (e.g.,
Get-Path /
as shorthand forGet-Path -Path /
).Only parameters that require a value (argument) can potentially be passed as values only - depending on the parameter declaration of the target command - in which case their order matters:
Value-only arguments are called positional arguments, and they bind in order to those parameters of the target command that are declared as positional, if any - see this answer for how to discover which of a given command's parameters are positional ones.
Prefixing a value by its target parameter (e.g.,
-LiteralPath /some/path
) makes it a named argument.A switch (flag) in PowerShell, such as
-Force
, is a special parameter type - Boolean in nature - that by definition requires passing it as a named argument, typically without a value (though you can attach a value, e.g.-Force:$true
or-Force:$false
- note that:
is then required to separate the parameter name from its value; see this answer for details).Since PowerShell allows named arguments to be specified in any order, the implication is that you're free to place by-definition-named switch arguments such as
-Force
anywhere on the command line.In short: Order only matters among positional (unnamed) arguments in PowerShell.
See the conceptual about_Parameters help topic for more information.
A path (parameter value in general) needs quoting:
if it contains PowerShell metacharacters, notably spaces; e.g.
C:\path\to\foo
needs no quoting, whereasC:\path with spaces\to\foo
does.if it starts with a subexpression (
$(...)
), in which case you need double-quoting, i.e."..."
(see below) - though you may choose to always use"..."
-quoting for paths involving subexpressions or variable references.Note that PowerShell has no concept that is the equivalent of the so-called shell expansions in POSIX-compatible shells such as Bash; therefore, whether a given argument is quoted or not makes no semantic difference (assuming it doesn't require quoting); as noted above,
*.txt
,'*.txt'
and"*.txt"
are all equivalent, and it is the target command, not PowerShell itself, that interprets the pattern - see this answer for more information.If quoting is needed:
Verbatim (single-quoted) strings (
'...'
) treat their content verbatimExpandable (double-quoted) strings (
"..."
) perform string interpolation ("expand" embedded variables and subexpressions, i.e replace them with their values).To use such variables as-is, no quoting is needed (even if the values contain spaces):
To make a variable (or expression) part of a larger string, embed it in an expandable (double-quoted) string (
"..."
); e.g.:Note:
Embedding the output from expressions or commands requires use of
$(...)
, the subexpression operator; e.g.Get-ChildItem "$(Get-Variable -ValueOnly Home)/Desktop"
; for a complete overview of PowerShell's expandable strings (string interpolation), see this answer.Situationally, such as in the example above, omitting the
"..."
quoting would work too - see this answer for more information.Additionally and alternatively, PowerShell allows you to use
(...)
, the grouping operator to pass the result of arbitrary expressions and commands as arguments; the following is the equivalent of the command above:Alternatively, using the
Join-Path
cmdlet: