如何在不使用 attrib.exe 的情况下通过 Powershell 更改扩展的 windows 文件属性?
How to change extended windows file attributes via Powershell without using attrib.exe?
这似乎是一个很简单的问题,但谷歌搜索没有给我任何结果。
这是错误 (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 调用制作包装器将是最后的选择。
我是否遗漏了设置这些扩展文件属性的任何其他[更简单]方法?
PS。除了 [System.IO.FileAttributes]::Temporary
我有兴趣设置 [System.IO.FileAttributes]::NotContentIndexed
.
您可以直接编辑 [FileInfo]
对象的属性 属性。例如,如果您想将 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 OS:
Get-ChildItem C:\Temp | ForEach{
$_.Attributes = [System.IO.FileAttributes]($_.Attributes.value__ + 8192)
}
这似乎是一个很简单的问题,但谷歌搜索没有给我任何结果。 这是错误 (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 调用制作包装器将是最后的选择。
我是否遗漏了设置这些扩展文件属性的任何其他[更简单]方法?
PS。除了 [System.IO.FileAttributes]::Temporary
我有兴趣设置 [System.IO.FileAttributes]::NotContentIndexed
.
您可以直接编辑 [FileInfo]
对象的属性 属性。例如,如果您想将 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 OS:
Get-ChildItem C:\Temp | ForEach{
$_.Attributes = [System.IO.FileAttributes]($_.Attributes.value__ + 8192)
}