What are the possible 'Mode' values returned by PowerShell's Get-ChildItem cmdlet?

后端 未结 4 393
孤独总比滥情好
孤独总比滥情好 2021-01-31 01:35

When I run PowerShell\'s Get-ChildItem on a directory (or any cmdlet that returns file system items), it shows a column called Mode, like this:

             


        
相关标签:
4条回答
  • 2021-01-31 02:06

    These are all the file attribute names and there meanings can be found here:

    PS C:\> [enum]::GetNames("system.io.fileattributes")
    ReadOnly
    Hidden
    System
    Directory
    Archive
    Device
    Normal
    Temporary
    SparseFile
    ReparsePoint
    Compressed
    Offline
    NotContentIndexed
    Encrypted
    
    0 讨论(0)
  • 2021-01-31 02:12

    Note that the mode you see is just a string representation of a bitfield enum that hides in the Attributes property. You can figure out what the individual letters mean by simply showing both side by side:

    PS> gci|select mode,attributes -u
    
    Mode                Attributes
    ----                ----------
    d-----               Directory
    d-r---     ReadOnly, Directory
    d----l Directory, ReparsePoint
    -a----                 Archive
    

    In any case, the full list is:

    d - Directory
    a - Archive
    r - Read-only
    h - Hidden
    s - System
    l - Reparse point, symlink, etc.
    
    0 讨论(0)
  • 2021-01-31 02:13

    IMHO, the most explanatory is the code itself:

    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 + "-");
    
    0 讨论(0)
  • 2021-01-31 02:18

    Calling these "attributes" is a Windows-specific name and breaks from *nix tradition of calling this "mode". I.e. man chmod for "change mode".

    It looks like Windows API design is bending (or acquiescing) toward the more popular term in the wider industry: "Mode".

    +1 from me.

    0 讨论(0)
提交回复
热议问题