.Net Attribute Flags

Attributes are bitwise combination of the following [System.IO.FileAttributes] fields:

ValueNameTargetsThe target…
0x000001 = 1ReadOnlyfile…is read-only.
0x000002 = 2Hiddenboth…is hidden, and thus is not included in an ordinary directory listing.
0x000004 = 4Systemboth…is a system file. That is, the file is part of the operating system or is used exclusively by the operating system.
0x000010 = 16Directorydirectory…is a directory.
0x000020 = 32Archiveboth…is marked to be included in incremental backup operation. Windows sets this attribute whenever the file is modified, and backup software should clear it when processing the file during incremental backup.
0x000040 = 64DevicereservedReserved for future use.
0x000080 = 128Normalfile…is a standard file that has no special attributes. This attribute is valid only if it is used alone.
0x000100 = 256Temporaryfile…is temporary. A temporary file contains data that is needed while an application is executing but is not needed after the application is finished. File systems try to keep all the data in memory for quicker access rather than flushing the data back to mass storage. A temporary file should be deleted by the application as soon as it is no longer needed.
0x000200 = 512SparseFilefile…is a sparse file. Sparse files are typically large files whose data consists of mostly zeros.
0x000400 = 1024ReparsePointboth…contains a reparse point, which is a block of user-defined data associated with a file or a directory.
0x000800 = 2048Compressedboth…is compressed.
0x001000 = 4096Offlinefile…is offline. The data of the file is not immediately available.
0x002000 = 8192NotContentIndexedboth…will not be indexed by the operating system’s content indexing service.
0x004000 = 16384Encryptedboth…is encrypted. For a file, this means that all data in the file is encrypted. For a directory, this means that encryption is the default for newly created files and directories.
0x008000 = 32768IntegrityStreamdirectory…includes data integrity support. When this value is applied to a file, all data streams in the file have integrity support. When this value is applied to a directory, all new files and subdirectories within that directory, by default, include integrity support.
0x010000 = 65536Virtual ¹⁾reserved
0x020000 = 131072NoScrubDataboth…is excluded from the data integrity scan. When this value is applied to a directory, by default, all new files and subdirectories within that directory are excluded from data integrity.
0x040000 = 262144EA ¹⁾internal…with extended attributes.
0x040000 = 262144Recall On Open ¹⁾internal…has no physical representation on the local system
0x080000 = 524288Pinned ¹⁾both…should be kept fully present locally even when not being actively accessed. Always keep on this device enables this attribute.
0x100000 = 1048576Unpinned ¹⁾both…should not be kept fully present locally except when being actively accessed. Free up space enables this attribute.
0x400000 = 4194304Recall On Data Access ¹⁾both…is not fully present locally.

¹⁾ Not recognized by Dotnet’s [IO.FileAttributes] but official Win32 file attribute

Example: combine read only and temporary

$flags = [IO.FileAttributes]::ReadOnly + [IO.FileAttributes]::Temporary

Modify Attributes

$info = Get-Item -Path ".\example.txt"
$info = New-TemporaryFile

Get attributes of a file

foreach ($n in 0..30) {
    [int]$value = [Math]::Pow(2, $n)
    if ([bool]($info.Attributes -band $value)) {
        [Enum]::Parse([IO.FileAttributes], $value) `
            -replace 524288, "Pinned" -replace 1048576, "Unpinned" `
            -replace 4194304, "RecallOnDataAccess"
    }
}

Test an attribute

[bool]($info.Attributes -band [IO.FileAttributes]::ReadOnly) # is read only?
[bool]($info.Attributes -band 0x080000) # is always available on this device?

Pin a file to keep it fully present locally

  • enable Pinned and disable Unpinned
$info.Attributes = $info.Attributes -bor 0x080000 -band (-bnot 0x100000)

Enable an attribute

$info.Attributes = $info.Attributes -bor [IO.FileAttributes]::Hidden

Disable an attribute

$info.Attributes = $info.Attributes -band (-bnot [IO.FileAttributes]::Hidden)

Toggle an attribute

$info.Attributes = $info.Attributes -bxor [IO.FileAttributes]::Hidden

Examples

Pin a folder and all its subfolders recursively to keep them fully present locally

$items = ".\mods\", ".\saves\"
@(
    Get-Item $items | where PSIsContainer | Get-ChildItem -Recurse
    Get-Item $items
) | foreach { 
    $info.Attributes = $info.Attributes -bor 0x080000 -band (-bnot 0x100000)
}

Display all attributes in a folder:

  • (Get-ChildItem).Attributes doesn’t work with ¹⁾ attributes
Get-ChildItem | foreach { 
    $value = [int]$_.Attributes
    [PSCustomObject]@{ Name = $_.Name; Value = $value; Attributes = $(
        0..30 | foreach { [Math]::Pow(2, $_) } | where {
            [bool]($_ -band $value) 
        } | foreach { 
            [Enum]::Parse([IO.FileAttributes], $_) `
                -replace 524288, "Pinned" -replace 1048576, "Unpinned" `
                -replace 4194304, "RecallOnDataAccess"
        }
    )
}}

Other

List all valid attributes:

0..30 | foreach { [Math]::Pow(2, $_) } | where {
    $_ -in [Enum]::GetValues([IO.FileAttributes])
} | foreach { [PSCustomObject]@{ 
    Decimal = $_
    Hexadecimal = $("0x" + [String]::Format("{0:X6}",[int]$_))
    Name = [Enum]::Parse([IO.FileAttributes], $_)
}}

Sources:

Related:

Tags:
File System - Use paths, get meta data, link, download, and encrypt files and folders