PowerShell的Get-ChildItem cmdlet返回的"模式"值有哪些?

Aar*_*sen 50 powershell

当我在目录(或任何返回文件系统项的cmdlet)上运行PowerShell的Get-ChildItem时,它会显示一个名为的列Mode,如下所示:

    Directory: C:\MyDirectory


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----          2/8/2011  10:55 AM            Directory1
d----          2/8/2011  10:54 AM            Directory2
d----          2/8/2011  10:54 AM            Directory3
-ar--          2/8/2011  10:54 AM        454 File1.txt
-ar--          2/8/2011  10:54 AM       4342 File2.txt
Run Code Online (Sandbox Code Playgroud)

我搜索并搜索了Google和我当地的PowerShell书籍,但我找不到任何有关该Mode列含义的文档.

Mode列的可能值是什么,每个值是什么意思?

Joe*_*oey 54

请注意,您看到的模式只是enum隐藏在Attributes属性中的位域的字符串表示形式.您可以通过简单地并排显示来确定单个字母的含义:

PS> gci|select mode,attributes -u

Mode                Attributes
----                ----------
d-----               Directory
d-r---     ReadOnly, Directory
d----l Directory, ReparsePoint
-a----                 Archive
Run Code Online (Sandbox Code Playgroud)

无论如何,完整列表是:

d - Directory
a - Archive
r - Read-only
h - Hidden
s - System
l - Reparse point, symlink, etc.
Run Code Online (Sandbox Code Playgroud)

  • 这不是完整列表。真正的要长得多:`[Enum]::GetValues("System.IO.FileAttributes")` (4认同)
  • @SN:此答案早于 Linux 上的 PowerShell,并且 Windows 没有可执行文件的文件系统属性。 (3认同)
  • PowerShell v5在链接(`ReparsePoint`)上有第六个点`l`. (2认同)
  • 可执行文件的“x”在哪里? (2认同)

ste*_*tej 7

恕我直言,最解释的是代码本身:

if (instance == null)
{
    return string.Empty;
}
FileSystemInfo baseObject = (FileSystemInfo) instance.BaseObject;
if (baseObject == null)
{
    return string.Empty;
}
string str = "";
if ((baseObject.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
{
    str = str + "d";
}
else
{
    str = str + "-";
}
if ((baseObject.Attributes & FileAttributes.Archive) == FileAttributes.Archive)
{
    str = str + "a";
}
else
{
    str = str + "-";
}
if ((baseObject.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
    str = str + "r";
}
else
{
    str = str + "-";
}
if ((baseObject.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
{
    str = str + "h";
}
else
{
    str = str + "-";
}
if ((baseObject.Attributes & FileAttributes.System) == FileAttributes.System)
{
    return (str + "s");
}
return (str + "-");
Run Code Online (Sandbox Code Playgroud)

  • 作为旁注:完整的属性列表比模式列中显示的五个标志大,并且它们是在对象上填充的属性.试试这个:`[Enum] :: GetValues("System.IO.FileAttributes")`... (2认同)

Jth*_*rpe 6

这些是所有文件属性名称,可以在此处找到含义:

PS C:\> [enum]::GetNames("system.io.fileattributes")
ReadOnly
Hidden
System
Directory
Archive
Device
Normal
Temporary
SparseFile
ReparsePoint
Compressed
Offline
NotContentIndexed
Encrypted
Run Code Online (Sandbox Code Playgroud)