如何在PowerShell中列出特定目录的内容?

发布于 2025-02-04 10:15:16 字数 470 浏览 2 评论 0 原文

我对Powershell一般感到困惑。很奇怪。无论如何,我已经阅读了:

PowerShell等于Linux's:ls -al -al

,所以我现在知道如何列出列出的内容。当前目录。但是如何列出任意目录的内容?

具体来说,

  • 如何输入要检查的路径?

    • 在Windows中, \ 是目录分隔符;但这也是大多数语言中的逃生炭。我该怎么办?
    • 我需要单价吗?双重报价?无报价?
  • 我将参数相对于交换机放在哪里?之后,就像我曾经或以前一样?

  • 如果我想使用环境变量或powershell变量作为列表路径的一部分 - 我该怎么做?

I'm rather baffled by Powershell in general. Very weird. Anyway, I've read:

Powershell's equivalent to Linux's: ls -al

so I now know how to list the contents of the current directory. But how can I list the contents of an arbitrary directory?

Specifically,

  • How do I type in the path I want to check?

    • in Windows, \ is the directory separator; but it's also an escape char in most languages. What do I do with it?
    • Do I need single-quotes? Double-quotes? No-quotes?
  • Where do I place the argument relative to the switches? After, like I'm used to, or rather before?

  • If I want to use an environment variable, or a powershell variable, as part of the path to list - how do I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮年慕年 2025-02-11 10:15:16

一般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 离线,您可能必须先安装本地帮助,如上所述。


示例

# The following commands all pass "\" (the [current drive's] root dir)
# to the -Path parameter.
# Note: 
#  * "\" is NOT special in PowerShell, so it needs no escaping.
#    PowerShell allows use of "/" instead even on Windows.
#  * -Path arguments are interpreted as wildcard patterns, whether
#     quoted or not. You may pass *multiple* paths / patterns.
#  * Switch -Force - which, as all switches - can be placed anywhere
#    among the arguments, requests that *hidden* items be listed too.

Get-ChildItem -Path \ -Force
Get-ChildItem \ -Force  # ditto, with *positional* param. binding
'\' | Get-ChildItem     # ditto, via the pipeline.
Get-ChildItem / -Force  # ditto, with "/" as alternative to "\"

# To force interpretation as a *literal* path - which matters for
# paths that contain "[" chars. - use -LiteralPath.
# Here too you may pass *multiple* paths.
Get-ChildItem -LiteralPath \ -Force

# Quoting is needed for paths with spaces, for instance.

# Single-quoting treats the string *verbatim*:
Get-ChildItem 'C:\path\to\dir with spaces'

# Double-quoting *expands* (interpolates) variable references
# and subexpressions.
Get-ChildItem "$HOME\dir with spaces"

# A variable alone can be used as-is, without double-quoting, even
# if its value contains spaces.
Get-ChildItem $HOME

回答您的特定问题,对于来自posix兼容壳的读者,例如bash :

如何在要检查的路径中输入?

提供两种指定一个或多个输入路径的方法,因为PowerShell中的大多数文件处理CMDET都可以:

在Windows中, \ 是目录分隔符;但这也是大多数语言中的逃生炭。我该怎么办?

在powershell \ 不是的逃脱字符,因此 \ 实例被视为文字,并且do not not 需要逃脱;它是`,所谓的 backtick 在powerShell中用作逃生字符 - 请参阅概念 about_special_characters 帮助主题。

但是,请注意,PowerShell通常允许您在路径互换中使用 \ / C:/Windows 正常工作。

我将参数相对于交换机放在哪里?之后,就像我曾经或以前一样?

注意:

  • 所有参数具有 name 在powershell 中 - 也就在 options 之间LS -L/)。

  • 。 em>可选(例如, 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 - 请注意,<代码>:是需要将参数名称与其值分开; >此答案有关详细信息)。

  • 顺便说一句:与符合POSIX的实用程序不同,PowerShell确实具有任何其他类型的可选值支持参数 - 请参见此答案

由于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>

如果需要引用:

如果我想使用环境变量或powershell变量作为列表路径的一部分 - 我该怎么做?

要使用这样的变量,不需要引用(即使值包含空格):

# PowerShell variable:
Get-ChildItem -LiteralPath $HOME

# Environment variable, e.g. on Windows:
Get-ChildItem -LiteralPath $env:USERPROFILE

要使较大的字符串的变量(或表达式)部分嵌入;例如:

Get-ChildItem -LiteralPath "$HOME/Desktop"

注意:

此外,PowerShell允许您使用(...)分组操作员将任意表达式和命令作为参数的结果;以上是上面命令的等效:

Get-ChildItem -LiteralPath ($HOME + '/Desktop')

或者,使用 JOIN-PATH cmdlet:

Get-ChildItem -LiteralPath (Join-Path $HOME Desktop)

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.

    • If you have offline help installed (true by default in Windows PowerShell; installable on demand via Update-Help in PowerShell (Core) 7+), you can also print the examples with Get-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 to Get-Command -Syntax (Get-ChildItem -? or Get-Command -Syntax Get-ChildItem). To also access the help text offline, you may have to install local help first, as described above.

Examples:

# The following commands all pass "\" (the [current drive's] root dir)
# to the -Path parameter.
# Note: 
#  * "\" is NOT special in PowerShell, so it needs no escaping.
#    PowerShell allows use of "/" instead even on Windows.
#  * -Path arguments are interpreted as wildcard patterns, whether
#     quoted or not. You may pass *multiple* paths / patterns.
#  * Switch -Force - which, as all switches - can be placed anywhere
#    among the arguments, requests that *hidden* items be listed too.

Get-ChildItem -Path \ -Force
Get-ChildItem \ -Force  # ditto, with *positional* param. binding
'\' | Get-ChildItem     # ditto, via the pipeline.
Get-ChildItem / -Force  # ditto, with "/" as alternative to "\"

# To force interpretation as a *literal* path - which matters for
# paths that contain "[" chars. - use -LiteralPath.
# Here too you may pass *multiple* paths.
Get-ChildItem -LiteralPath \ -Force

# Quoting is needed for paths with spaces, for instance.

# Single-quoting treats the string *verbatim*:
Get-ChildItem 'C:\path\to\dir with spaces'

# Double-quoting *expands* (interpolates) variable references
# and subexpressions.
Get-ChildItem "$HOME\dir with spaces"

# A variable alone can be used as-is, without double-quoting, even
# if its value contains spaces.
Get-ChildItem $HOME

To answer your specific questions, for readers who come from POSIX-compatible shells such as Bash:

How do I type in the path I want to check?

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.

    • Note that - unlike in POSIX-compatible shells such as Bash - it does not matter whether the path is unquoted or not; e.g., Get-ChildItem -Path *.txt, Get-ChildItem "*.txt", and Get-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/or System.IO.DirectoryInfo instances (such as output by (another) Get-ChildItem or Get-Item call) via the pipeline DOES bind to -LiteralPath, as explained in this answer.

in Windows, \ is the directory separator; but it's also an escape char in most languages. What do I do with it?

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.

Where do I place the argument relative to the switches? After, like I'm used to, or rather before?

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 / in ls -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 for Get-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).

  • As an aside: Unlike POSIX-compliant utilities, PowerShell does not support parameters with optional values of any other type - see this answer.

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.

Do I need single-quotes? Double-quotes? No-quotes?

A path (parameter value in general) needs quoting:

  • if it contains PowerShell metacharacters, notably spaces; e.g. C:\path\to\foo needs no quoting, whereas C:\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:

If I want to use an environment variable, or a powershell variable, as part of the path to list - how do I do that?

To use such variables as-is, no quoting is needed (even if the values contain spaces):

# PowerShell variable:
Get-ChildItem -LiteralPath $HOME

# Environment variable, e.g. on Windows:
Get-ChildItem -LiteralPath $env:USERPROFILE

To make a variable (or expression) part of a larger string, embed it in an expandable (double-quoted) string ("..."); e.g.:

Get-ChildItem -LiteralPath "$HOME/Desktop"

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:

Get-ChildItem -LiteralPath ($HOME + '/Desktop')

Alternatively, using the Join-Path cmdlet:

Get-ChildItem -LiteralPath (Join-Path $HOME Desktop)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文