Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change a file's attribute using Powershell?

Tags:

I have a Powershell script that copies files from one location to another. Once the copy is complete I want to clear the Archive attribute on the files in the source location that have been copied.

How do I clear the Archive attribute of a file using Powershell?

like image 355
Mark Capaldi Avatar asked Jan 21 '09 10:01

Mark Capaldi


People also ask

How do you change file attributes?

To view or change the attributes of a file, right-click the file, and then click Properties. In the "Attributes:" section, enabled attributes have checks beside them. Add or remove the checks from Read-only, Archive, or Hidden to enable or disable these options.

How do I change Properties in PowerShell?

To access those settings, click the PowerShell icon in the top-left corner of the console window and click Properties to open the Properties dialog box. The Properties dialog box includes four tabs—Options, Font, Layout, and Colors—each of which contain configuration settings that you can modify as necessary.

Which command would you use to change file attributes?

The MS-DOS "Attrib" command allows you to add and remove attributes from a file -- even if the file is hidden or you cannot start Windows. You can also use this command to modify the attributes of a system file, archive file or read-only file -- or to see the attributes of a file without modifying anything.


2 Answers

You can use the good old dos attrib command like this:

attrib -a *.* 

Or to do it using Powershell you can do something like this:

$a = get-item myfile.txt $a.attributes = 'Normal' 
like image 188
Simon Steele Avatar answered Sep 23 '22 11:09

Simon Steele


As the Attributes is basically a bitmask field, you need to be sure clear the archive field while leaving the rest alone:

 PS C:\> $f = get-item C:\Archives.pst PS C:\> $f.Attributes Archive, NotContentIndexed PS C:\> $f.Attributes = $f.Attributes -band (-bnot [System.IO.FileAttributes]::Archive) PS C:\> $f.Attributes NotContentIndexed PS H:\> 
like image 41
Goyuix Avatar answered Sep 21 '22 11:09

Goyuix