Hi, I am reading a book on PowerShell. I am confused on a particular code on attributes.
The attribute to the file has both “Archive” and “ReadOnly”. What am confused about is that where in this code that it deleted the attribute of “ReadOnly” and how did it do it?
Get File Attributes
$file = get-item –path “c:\Program Files\MyCustomSoftware\Graphics\FirstGraphic.png”
$attributes = $file.attributes
Convert attributes to string
$attributes = $attributes.tostring()
Split individual attributes into array
$attributes = $attributes.split(“,”)
Read through the individual attributes
Foreach ($attribute in $attributes) {
If read Only, skip
if ($attribute –like “ReadOnly”) {
write-host “Skipping Attribute: $attribute”
}
# Else add attribute to attribute list
else {
$newattribute += “$attribute,”
Write-Host “Attribute Added: $attribute”
}
}
Remove the trailing comma
$newattribute = $newattribute.trimend(“,”)
Write over existing attributes with new attributes
$file.attributes = $newattribute
Write-host "New File attributes are: " $file.attributes