[英]How to list contents of a specific directory in powershell?
I'm rather baffled by Powershell in general.总的来说,我对 Powershell 感到困惑。 Very weird.
很奇怪。 Anyway, I've read:
无论如何,我读过:
Powershell's equivalent to Linux's: ls -al Powershell 等效于 Linux: 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?如何输入要检查的路径?
\
is the directory separator;\
是目录分隔符; but it's also an escape char in most languages. 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?如果我想使用环境变量或 powershell 变量作为列表路径的一部分 - 我该怎么做?
General PowerShell information and examples:一般 PowerShell 信息和示例:
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: PowerShell 原生命令(包括选择加入的用户编写的命令)具有标准化的参数语法和绑定规则,因此以下主要针对
Get-ChildItem
的命令通常适用:
See the help topic for Get-ChildItem
, which describes the command's purpose, syntax, and individual parameters, along with showing examples.请参阅
Get-ChildItem
的帮助主题,其中描述了命令的用途、语法和各个参数,并显示了示例。
Update-Help
in PowerShell (Core) 7+), you can also print the examples with Get-Help -Example Get-ChildItem | more
Update-Help
按需安装),您还可以使用Get-Help -Example Get-ChildItem | more
打印示例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.至于如何通常阅读描述 cmdlet 帮助主题的
SYNTAX
标题下列出的受支持参数的表示法,PowerShell 称之为语法图,请参阅概念about_Command_Syntax帮助主题。
Offline, you can print the syntax diagrams for PowerShell-native commands with standard switch -?
离线,您可以使用标准开关打印 PowerShell 原生命令的语法图
-?
or by passing the command name to Get-Command -Syntax
( Get-ChildItem -?
or Get-Command -Syntax Get-ChildItem
).或通过将命令名称传递给
Get-Command -Syntax
( Get-ChildItem -?
或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 :要回答您的具体问题,对于来自 POSIX 兼容外壳(例如 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: Get-ChildItem
提供了两种方法来指定一个或多个输入路径,就像 PowerShell 中的大多数文件处理 cmdlet 一样:
-Path
accepts one or more names or paths that are interpreted as a wildcard pattern. -Path
接受一个或多个名称或路径,这些名称或路径被解释为通配符模式。
Get-ChildItem -Path *.txt
, Get-ChildItem "*.txt"
, and Get-ChildItem '*.txt'
are all equivalent;Get-ChildItem -Path *.txt
、 Get-ChildItem "*.txt"
和Get-ChildItem '*.txt'
都是等价的; 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
).-Path
,这使得"*.txt"
和'*.txt'
在位置上绑定到第一个位置参数-Path
)。 -LiteralPath
accepts one or more names or paths that are assumed to refer to existing file-system items literally (verbatim). -LiteralPath
接受一个或多个名称或路径,这些名称或路径被假定为逐字(逐字)引用现有文件系统项。
Given that literal paths on Unix-like platforms usually do not contain *
and ?
鉴于类 Unix 平台上的文字路径通常不包含
*
和?
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 (eg [ab]
and [0-9]
).字符和在 Windows上不能,使用
-LiteralPath
进行歧义消歧通常只需要包含[
(可能还有]
)的路径,因为PowerShell 的通配符模式语言也支持字符集和范围(例如[ab]
和[0-9]
)。
Binding to -LiteralPath
via an argument requires explicit use of -LiteralPath
, ie use of a named argument;通过参数绑定到
-LiteralPath
需要显式使用-LiteralPath
,即使用命名参数; eg, Get-ChildItem -LiteralPath foo
例如,
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 .但是,通过管道提供
System.IO.FileInfo
和/或System.IO.DirectoryInfo
实例(例如(另一个) Get-ChildItem
或Get-Item
调用的输出)确实绑定到-LiteralPath
,如本答案中所述。
in Windows,
\
is the directory separator;在 Windows 中,
\
是目录分隔符; 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;在 PowerShell 中
\
不是转义字符,因此\
实例被视为文字并且不需要转义; it is `
, the so-called backtick that serves as the escape character in PowerShell - see the conceptual about_Special_Characters help topic.它是
`
,在 PowerShell 中用作转义字符的所谓反引号- 请参阅概念about_Special_Characters帮助主题。
Note, however, that PowerShell generally allows you to use \
and /
in paths interchangeably , so that, eg, Get-ChildItem C:/Windows
works just fine.但是请注意,PowerShell 通常允许您在路径中互换使用
\
和/
,这样,例如Get-ChildItem C:/Windows
就可以正常工作。
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 (eg -l
and operands (value-only arguments, such as the /
in ls -l /
).所有参数在 PowerShell 中都有名称- 也就是说,选项之间没有类似 POSIX 的区别(例如
-l
和操作数(仅值参数,例如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 (eg, Get-Path /
as shorthand for Get-Path -Path /
).命令可以声明也可以仅按值传递的参数,在位置上,这意味着在值前面加上参数名称是可选的(例如,
Get-Path /
作为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 (eg, -LiteralPath /some/path
) makes it a named argument.通过目标参数(例如
-LiteralPath /some/path
)为值添加前缀使其成为命名参数。
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, eg -Force:$true
or -Force:$false
- note that :
is then required to separate the parameter name from its value; see this answer for details). PowerShell 中的开关(标志),例如
-Force
,是一种特殊的参数类型 - 本质上是布尔值 - 根据定义,它需要将其作为命名参数传递,通常没有值(尽管您可以附加值,例如-Force:$true
或-Force:$false
- 请注意:
然后需要将参数名称与其值分开;有关详细信息,请参阅此答案)。
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.由于 PowerShell 允许以任何顺序指定命名参数,这意味着您可以在命令行的任何位置随意放置按定义命名的开关参数,例如
-Force
。
In short: Order only matters among positional (unnamed) arguments in PowerShell.简而言之:顺序仅在 PowerShell 中的位置(未命名)参数中很重要。
See the conceptual about_Parameters help topic for more information.有关详细信息,请参阅概念about_Parameters帮助主题。
Do I need single-quotes?
我需要单引号吗? Double-quotes?
双引号? No-quotes?
没有引号?
A path (parameter value in general) needs quoting:路径(一般的参数值)需要引用:
if it contains PowerShell metacharacters, notably spaces ;如果它包含 PowerShell 元字符,尤其是空格; eg
C:\path\to\foo
needs no quoting, whereas C:\path with spaces\to\foo
does.例如
C:\path\to\foo
不需要引用,而C:\path with spaces\to\foo
则需要。
if it starts with a subexpression ( $(...)
) , in which case you need double -quoting, ie "..."
(see below) - though you may choose to always use "..."
-quoting for paths involving subexpressions or variable references.如果它以子表达式 (
$(...)
)开头,在这种情况下您需要双引号,即"..."
(见下文) - 尽管您可以选择始终使用"..."
-quoting for涉及子表达式或变量引用的路径。
Note that PowerShell has no concept that is the equivalent of the so-called shell expansions in POSIX-compatible shells such as Bash;请注意, PowerShell没有与 POSIX 兼容的 shell(如 Bash)中的所谓 shell 扩展等效的概念; 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.如上所述,
*.txt
、 '*.txt'
和"*.txt"
都是等价的,并且解释模式的是目标命令,而不是 PowerShell 本身 - 请参阅此答案以获取更多信息。
If quoting is needed:如果需要报价:
Verbatim (single-quoted) strings ( '...'
) treat their content verbatim 逐字(单引号)字符串(
'...'
)逐字处理其内容
Expandable (double-quoted) strings ( "..."
) perform string interpolation ("expand" embedded variables and subexpressions, ie replace them with their values). 可扩展(双引号)字符串(
"..."
)执行字符串插值(“扩展”嵌入变量和子表达式,即用它们的值替换它们)。
If I want to use an environment variable, or a powershell variable, as part of the path to list - how do I do that?
如果我想使用环境变量或 powershell 变量作为列表路径的一部分 - 我该怎么做?
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 ( "..."
) ;要使变量(或表达式)成为较大字符串的一部分,请将其嵌入可扩展(双引号)字符串(
"..."
)中; eg:例如:
Get-ChildItem -LiteralPath "$HOME/Desktop"
Note:笔记:
Embedding the output from expressions or commands requires use of $(...)
, the subexpression operator ;嵌入表达式或命令的输出需要使用
$(...)
, 子表达式运算符; eg例如
Get-ChildItem "$(Get-Variable -ValueOnly Home)/Desktop"
; Get-ChildItem "$(Get-Variable -ValueOnly Home)/Desktop"
; for a complete overview of PowerShell's expandable strings (string interpolation), see this answer .有关 PowerShell 的可扩展字符串(字符串插值)的完整概述,请参阅此答案。
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;此外,PowerShell 允许您使用
(...)
, 分组运算符将任意表达式和命令的结果作为参数传递; the following is the equivalent of the command above:以下是上述命令的等效项:
Get-ChildItem -LiteralPath ($HOME + '/Desktop')
Alternatively, using the Join-Path
cmdlet:或者,使用
Join-Path
cmdlet:
Get-ChildItem -LiteralPath (Join-Path $HOME Desktop)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.