3/23/2022

如何利用 Powershell 設定檔案屬性

 Powershell 提供兩種方式設定檔案屬性的設定值。

  • 屬性名稱表示
  • $file = Get-Item .\x.exe
    $file.attributes = "ReadOnly"

  • 屬性數值表示(16位元數值)
  • $file = Get-Item .\x.exe
    $file.attributes = 0x20

兩者的對照表如下:
屬性名稱
屬性數值
ReadOnly
0x01
Hidden
0x02
System
0x04
Directory
0x10
Archive
0x20
Device
0x40
Normal
0x80
Temporary
0x100
SparseFile
0x200
ReparsePoint
0x400
Compressed
0x800

依照以上的說明,我們也可以同時設定兩組以上屬性。
  • 屬性名稱表示
  • $file = Get-Item .\x.exe
    $file.attributes = "ReadOnly,Archive"

  • 屬性數值表示(16位元數值)
  • $file = Get-Item .\x.exe
    $file.attributes = 0x21