这似乎是一个非常简单的问题,但谷歌搜索没有给我任何结果。 这是错误(PS 5.1,win 10.0.14393 x64):
Set-ItemProperty $myFileInfo -Name Attributes -Value ([System.IO.FileAttributes]::Temporary)
The attribute cannot be set because attributes are not supported. Only the following attributes can be set: Archive, Hidden, Normal, ReadOnly, or System.
attrib.exe
似乎支持大部分 System.IO.FileAttributes
。不幸的是,它似乎不适用于使用 FileSystem PSDrives 引用的文件。这就是我广泛使用的。
为 SetFileAttributes 制作包装器内核 API 调用将是最后的手段。
我是否缺少设置这些扩展文件属性的任何其他[更简单]方法?
附言。除了 [System.IO.FileAttributes]::Temporary
我对设置 [System.IO.FileAttributes]::NotContentIndexed
很感兴趣。
最佳答案
您可以直接编辑[FileInfo]
对象的Attributes 属性。例如,如果您想将 C:\Temp 文件夹中的所有文件排除在内容索引之外,您可以这样做:
Get-ChildItem C:\Temp | ForEach{
$_.Attributes = $_.Attributes + [System.IO.FileAttributes]::NotContentIndexed
}
这将获取每个文件,然后将 [System.IO.FileAttributes]::NotContentIndexed
属性添加到现有属性。您可能会过滤文件以确保该属性在尝试添加之前不存在,因为这可能会引发错误(我不知道,我没试过)。
编辑:如@grunge 所述,这在 Windows Server 2012 R2 中不起作用。相反,您需要做的是引用 value__
属性,这是按位标志值,并为 NotContentIndexed
添加按位标志。这应该适用于任何 Windows 操作系统:
Get-ChildItem C:\Temp | ForEach{
$_.Attributes = [System.IO.FileAttributes]($_.Attributes.value__ + 8192)
}
关于powershell - 如何在不使用 attrib.exe 的情况下通过 Powershell 更改扩展的 Windows 文件属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42028926/