fbpx Manage and Change the Attributes of a File | Windows PowerShell, Windows Server | HostingUltraso.com
24/7/365 Support

Manage and Change the Attributes of a File

Problem

You want to update the ReadOnly, Hidden, or System attributes of a file.

Solution

Most of the time, you will want to use the familiar attrib.exe program to change the attributes of a file:

attrib +r test.txt

attrib s test.txt To set only the ReadOnly attribute, you can optionally set the IsReadOnly property on the file:

$file = GetItem test.txt $file.IsReadOnly = $true

To apply a specific set of attributes, use the Attributes property on the file:

$file = GetItem test.txt $file.Attributes = "ReadOnlyNotContentIndexed"

Directory listings show the attributes on a file, but you can also access the Mode or Attributes property directly:

PS >$file.Attributes = "ReadOnly","System","NotContentIndexed" PS >$file.Mode rs PS >$file.Attributes ReadOnly, System, NotContentIndexed

Discussion

When the GetItem or GetChildItem cmdlets retrieve a file, the resulting file has an Attributes property. This property doesn’t offer much in addition to the regular attrib.exe program, although it does make it easier to set the attributes to a specific state.

Be aware that setting the Hidden attribute on a file removes it from most default views. If you want to retrieve it after hiding it, most commands require a –Force parameter. Similarly, setting the ReadOnly

attribute on a file causes most write operations on that file to fail unless you call that command with the –Force parameter.

If you want to add an attribute to a file using the Attributes property (rather than attrib.exe for some reason), this is how you would do that:

$file = GetItem test.txt $readOnly = [IO.FileAttributes] "ReadOnly" $file.Attributes = $file.Attributes bor $readOnly

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say