How-to: Change file attributes in PowerShell.

The PowerShell scripts below can be used to get, set or remove file attributes.

In PowerShell the constant values for all the various file attributes are available via [io.fileattributes]
for example[int][io.fileattributes]::ReadOnly will return 1.

The scripts below use these constants and perform some binary arithmetic to add or remove each attribute from a file system object.
They retrieve the files using Get-ChildItem -force which ensures they will work even if the file is hidden.

[set-attrib.ps1]

param (
   [string]$FilePath = $(throw "-FilePath is required."),
   [switch]$Archive = $false,
   [switch]$ReadOnly = $false,
   [switch]$Hidden = $false,
   [switch]$System = $false
)

$ARCHIVE_ATTRIB = [io.fileattributes]::archive #32
$READONLY_ATTRIB = [io.fileattributes]::ReadOnly #1
$HIDDEN_ATTRIB = [io.fileattributes]::Hidden  #2
$SYSTEM_ATTRIB = [io.fileattributes]::System  #4

$files = Get-ChildItem -Path $FilePath -Recurse -force

Foreach($file in $files) {
 If ($Archive){
    Set-ItemProperty -Path $file.fullname -Name attributes `
      -Value ((Get-ItemProperty $file.fullname).attributes -BOR $ARCHIVE_ATTRIB)
 }
 If ($ReadOnly){
    Set-ItemProperty -Path $file.fullname -Name attributes `
      -Value ((Get-ItemProperty $file.fullname).attributes -BOR $READONLY_ATTRIB)
 }
 If ($Hidden){
    Set-ItemProperty -Path $file.fullname -Name attributes `
      -Value ((Get-ItemProperty $file.fullname).attributes -BOR $HIDDEN_ATTRIB)
 }
 If ($System){
    Set-ItemProperty -Path $file.fullname -Name attributes `
      -Value ((Get-ItemProperty $file.fullname).attributes -BOR $SYSTEM_ATTRIB)
 }
} #end Foreach

[remove-attrib.ps1]

param (
   [string]$FilePath = $(throw "-FilePath is required."),
   [switch]$Archive = $false,
   [switch]$ReadOnly = $false,
   [switch]$Hidden = $false,
   [switch]$System = $false
)

$ARCHIVE_ATTRIB = [io.fileattributes]::archive #32
$READONLY_ATTRIB = [io.fileattributes]::ReadOnly #1
$HIDDEN_ATTRIB = [io.fileattributes]::Hidden  #2
$SYSTEM_ATTRIB = [io.fileattributes]::System  #4

$files = Get-ChildItem -Path $FilePath -Recurse -force

Foreach($file in $files) {
 If ($Archive -and ((Get-ItemProperty -Path $file.fullname).attributes -band $ARCHIVE_ATTRIB)) {
    Set-ItemProperty -Path $file.fullname -Name attributes `
     -Value ((Get-ItemProperty $file.fullname).attributes -BXOR $ARCHIVE_ATTRIB)
 }
 If ($ReadOnly -and ((Get-ItemProperty -Path $file.fullname).attributes -band $READONLY_ATTRIB)) {
    Set-ItemProperty -Path $file.fullname -Name attributes `
     -Value ((Get-ItemProperty $file.fullname).attributes -BXOR $READONLY_ATTRIB)
  }
 If ($Hidden -and ((Get-ItemProperty -Path $file.fullname).attributes -band $HIDDEN_ATTRIB)) {
    Set-ItemProperty -Path $file.fullname -Name attributes `
     -Value ((Get-ItemProperty $file.fullname).attributes -BXOR $HIDDEN_ATTRIB)
 }
 If ($System -and ((Get-ItemProperty -Path $file.fullname).attributes -band $SYSTEM_ATTRIB)) {
    Set-ItemProperty -Path $file.fullname -Name attributes `
     -Value ((Get-ItemProperty $file.fullname).attributes -BXOR $SYSTEM_ATTRIB)
  }
} #end Foreach

[get-attrib.ps1]

param (
   [string]$FilePath = $(throw "-FilePath is required.")
)
# Pass either a single filename or a wildcard
#  get-attrib C:\demo\*.txt

$ARCHIVE_ATTRIB = [io.fileattributes]::archive #32
$READONLY_ATTRIB = [io.fileattributes]::ReadOnly #1
$HIDDEN_ATTRIB = [io.fileattributes]::Hidden  #2
$SYSTEM_ATTRIB = [io.fileattributes]::System  #4

$files = Get-Item -Path $FilePath -force

if ($files.count -gt 1) {
   $files = Get-ChildItem -Path $FilePath -Recurse -force
}

Foreach($file in $files) {
$attribs = ""
 If (((Get-ItemProperty -Path $file.fullname).attributes -band $ARCHIVE_ATTRIB) -eq $ARCHIVE_ATTRIB){
    $attribs = "| Archive"
 }
 If (((Get-ItemProperty -Path $file.fullname).attributes -band $READONLY_ATTRIB) -eq 1){
    $attribs = "$attribs | Read-only"
 }
 If (((Get-ItemProperty -Path $file.fullname).attributes -band $HIDDEN_ATTRIB) -eq 2){
    $attribs = "$attribs | Hidden"
 }
 If (((Get-ItemProperty -Path $file.fullname).attributes -band $SYSTEM_ATTRIB) -eq 4){
    $attribs = "$attribs | System"
 }
 if ($attribs -eq ""){$attribs = "| Normal"}
 "$file $attribs"
} #end Foreach

Examples

Add the ReadOnly and Hidden attributes on the file 'C:\logs\monday.csv'

PS C:\demo> ./set-attrib.ps1 "C:\logs\monday.csv" -readonly -hidden

Add the Archive attributes on all the .TXT files in the C:\logs\ folder:

PS C:\demo> ./set-attrib.ps1 "C:\logs\*.txt" -archive

Remove the Archive, ReadOnly and Hidden attributes on all files in the C:\logs\ folder:

PS C:\demo> ./remove-attrib.ps1 "C:\logs\*.*" -archive -readonly -hidden

“We often dismiss controversies or concerns by waving our hands and saying something like, “Oh, that’s merely symbolic,” as if the meaning we give to symbols is somehow irrelevant compared with more tangible things. But symbolism — the way we reduce broad concerns, agendas, and visions to images or rituals — has played a defining role in human life since there have been humans. Try burning a flag or a cross in front of the wrong audience and then tell me symbolism is nothing” ~ Jonah Goldberg

Related PowerShell Cmdlets:

ATTRIB - Display or change file attributes.


 
Copyright © 1999-2021 SS64.com
Some rights reserved