This article describes how to set file properties in a PowerShell. We know that the properties of the file are read-only, hidden, system, archive and no content index, etc. 5, read-only and hidden use more, the other three are used relatively little.
The properties of a file
I do not know if you have used attrib.exe this cmd applet, it can be used to set the properties of the file. We suggest that we first go to understand this attrib small program. You can refer to the article: use attrib to set file read-only, hide, and System Properties
Let's look at how to set the file read-only, hidden, and System properties in PowerShell.
We know that getting a file object can use Get-item this cmdlet. But let's take a look at the following actions:
Copy Code code as follows:
PS c:\users\splaybow> Get-item D:\2.txt
Get-item: Item D:\2.txt not found.
Location: 1 Characters: 9
+ Get-item <<<< d:\2.txt
+ Categoryinfo:objectnotfound: (d:\2.txt:string) [Get-item], IO
Exception
+ Fullyqualifiederrorid:itemnotfound,microsoft.powershell.commands.getit
Emcommand
This hint seems to tell us clearly that we can't find the D:\2.txt file. Don't you really have this file? No! This file was previously set by me to hide the properties of the system properties, and I'll try to remove it.
Copy Code code as follows:
PS c:\users\splaybow> attrib-h-S D:\2.txt
PS c:\users\splaybow> Get-item D:\2.txt
Table of Contents: D:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-ar--2013/11/11 20:04 6 2.txt
That's when you find the file. In addition, you can see the "mode" part of the output, "AR" indicates that there are also archive attributes and read-only properties on the file. We can also view the properties of the file by using the following command:
Copy Code code as follows:
PS c:\users\splaybow> $file = Get-item d:\2.txt
PS c:\users\splaybow> $file. Mode
-ar--
About the file five kinds of attributes, Hongo no longer introduced, interested friends can refer to attrib's article.
Second, set file properties
Now we test to remove the read-only attribute on the file and use the IsReadOnly property of the file object.
Copy Code code as follows:
PS c:\users\splaybow> $file. IsReadOnly = $false
PS c:\users\splaybow> $file. Mode
-A---
From this result, is not r (read only) no?! To add back, then the isreadonly set to $true can be.
Congmingruhong, must think, hidden attributes can be $file object Ishidden to control it? Oh, we think wrong. To find out what properties and methods the $file object has, you can use the $file | Get-member "This command to view. Of course, any object, we want to know what methods and attributes it has, can all pass "< object variables > | Get-memeber "Way to get.
Let's not go far. How to set other attributes that are read-only, then you can only use the Attributes property. And look at the following example:
Copy Code code as follows:
PS c:\users\splaybow> $file. Attributes = "Readonly", "System", "notcontentindexed", "hidden", "archive"
PS c:\users\splaybow> $file. Mode
-arhs
PS c:\users\splaybow> $file. Attributes
ReadOnly, Hidden, System, Archive, notcontentindexed
The above example shows how to set a file's read-only properties, hidden properties, System Properties, archive properties, no content index properties, and so on. Note that there is a difference between the mode attribute and the Attributes property, which is that the No Content archive attribute (I) appears in mode.
Third, modify file properties
The above is a one-time set of properties for the file, this is simple, but also very rough. If you just want to add a readonly attribute to him, the other original properties unchanged, how to operate?
In PowerShell, you can use the properties of a file as a binary array, using a binary bitwise OR (BOR) operator. and see examples:
Copy Code code as follows:
PS c:\users\splaybow> $file = Get-item d:\2.txt
PS c:\users\splaybow> $file. Attributes= "Hidden"
PS c:\users\splaybow> $readonly = [System.IO.FileAttributes] "ReadOnly"
PS c:\users\splaybow> $file. Attributes= $file. Attributes-bor $readonly
PS c:\users\splaybow> $file. Attributes
ReadOnly, Hidden
In the example above, the file itself has only one hidden attribute, followed by a read-only property, and the result is changed to hidden, read-only dual properties. What if you want to remove one of the attributes? Unfortunately, there is no bitwise non-operator, how to achieve the left to think.
About PowerShell settings file read-only, hidden and system properties, this article on the introduction of so many, I hope to help you, thank you!