当名称以〜开头时,Powershell设置属性sh

时间:2016-05-29 13:51:18

标签: powershell attributes filenames hidden

我需要编写一个通过几百个目录递归的脚本,如果找到一个以"〜"开头的文件,则应该在该文件中设置hidden和system属性。

到目前为止,我得到了这个:

Get-ChildItem C:\test\~* - Recurse | foreach {$_.Attributes = 'Hidden, System'}

但它似乎只是改变了第一个文件。

2 个答案:

答案 0 :(得分:1)

  • 使用-Filter参数查找以~开头的文件。
  • 添加-File开关以排除目录。
  • 删除-Recurse
  • 之间的空格

这应该有效:

Get-ChildItem 'C:\test\' -Filter '~*' -Recurse -File | foreach {
    $_.Attributes = 'Hidden, System'
}

答案 1 :(得分:-1)

执行类似dir c:\ -recurse | ? Name -match "~"之类的操作,可以使用~in。然后像你一样设置属性。

相关问题