如何使用Powershell更改文件的属性?

Mar*_*ldi 22 powershell

我有一个Powershell脚本,可以将文件从一个位置复制到另一个位置.复制完成后,我想清除源位置中已复制文件的存档属性.

如何使用Powershell清除文件的Archive属性?

Sim*_*ele 26

您可以使用这样的旧的dos attrib命令:

attrib -a *.*
Run Code Online (Sandbox Code Playgroud)

或者使用Powershell执行此操作,您可以执行以下操作:

$a = get-item myfile.txt
$a.attributes = 'Normal'
Run Code Online (Sandbox Code Playgroud)


Mit*_*eat 11

这里:

function Get-FileAttribute{
    param($file,$attribute)
    $val = [System.IO.FileAttributes]$attribute;
    if((gci $file -force).Attributes -band $val -eq $val){$true;} else { $false; }
} 


function Set-FileAttribute{
    param($file,$attribute)
    $file =(gci $file -force);
    $file.Attributes = $file.Attributes -bor ([System.IO.FileAttributes]$attribute).value__;
    if($?){$true;} else {$false;}
} 
Run Code Online (Sandbox Code Playgroud)


Goy*_*uix 11

由于Attributes基本上是一个位掩码字段,因此您需要确保清除归档字段,同时保留其余字段:

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:\>