Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PowerShell隐藏文件夹_Powershell - Fatal编程技术网

使用PowerShell隐藏文件夹

使用PowerShell隐藏文件夹,powershell,Powershell,我的磁盘驱动器上有一个主文件夹,其中包含50个其他文件夹。现在,我想隐藏那50个文件夹。我可以简单地右键单击并选择“隐藏”属性复选框,但我正在寻找一种更快的方法。有什么建议吗 Windows 8。对于单个文件,您可以更改属性属性,如下所示: $f=get-item .\your_folder -Force $f.attributes="Hidden" Get-ChildItem -path "your_path_here" -Recurse -Force | foreach {$_.attri

我的磁盘驱动器上有一个主文件夹,其中包含50个其他文件夹。现在,我想隐藏那50个文件夹。我可以简单地右键单击并选择“隐藏”属性复选框,但我正在寻找一种更快的方法。有什么建议吗


Windows 8。

对于单个文件,您可以更改
属性
属性,如下所示:

$f=get-item .\your_folder -Force
$f.attributes="Hidden"
Get-ChildItem -path "your_path_here" -Recurse -Force | foreach {$_.attributes = "Hidden"}
要隐藏文件夹中的所有内容,可以使用
Get ChildItem
,如下所示:

$f=get-item .\your_folder -Force
$f.attributes="Hidden"
Get-ChildItem -path "your_path_here" -Recurse -Force | foreach {$_.attributes = "Hidden"}

被接受的答案提出了一个潜在的安全问题。

通过覆盖整个
属性
位字段,请注意先前在文件或文件夹中定义的任何其他属性(只读,加密,…)将被静默删除

使用二进制或二进制文件来防止此行为:

Get-Item .\your_folder -Force | foreach { $_.Attributes = $_.Attributes -bor "Hidden" }

其他两个答案都有问题。下面是一个答案,它根据注释(短符号和长符号)修复了这些问题:

gci -r $folder | % { $_.Attributes = $_.Attributes -bor "Hidden" }
# alternatively
Get-ChildItem -Recurse $folder | ForEach-Object { $_.Attributes = $_.Attributes -bor "Hidden" }
说明:

  • (别名
    gci
    ls
    )列出文件夹中的文件。
    -Recurse
    (别名
    -r
    )还列出了子文件夹中的文件、子文件夹的子文件夹等。其他答案中的
    -Force
    参数实际上并不需要:它的目的是列出已经隐藏的文件,如果您想在以后隐藏它,这是毫无意义的。
    但是,如果要取消隐藏文件,则需要添加
    -Force
    参数,以便获得如下命令(简写为notation)

  • (别名
    %
    foreach
    )迭代输入对象。它的参数是一个脚本块
    {…}
    ,其中当前对象是特殊变量
    $\uu

  • 在该脚本块中,
    $\uu
    是一个脚本。其参数包含文件的属性。这是一个很好的例子。其中一个可能的属性是“隐藏”(枚举中的第二位,但并不重要)

  • 将“隐藏”位添加到枚举中。请注意,
    =
    操作符将覆盖该文件的所有其他属性,这可能会有问题


  • 我投票决定关闭它,因为它更适合超级用户。你根本不是在问编程问题。正如我提到的,我至少有50-60个文件夹需要隐藏。如果我在父文件夹中运行此命令,它会将隐藏属性添加到所有子文件夹吗?我只是测试了它,第一个解决方案它不是递归的。您可以使用
    Get ChildItem-path“your_path_here”-Recurse-Force | foreach{$\uu.attributes=“Hidden”}
    执行递归更改。我用这些信息更新了答案。$f.Attributes+=“Hidden”不应该是Get ChildItem吗?
    +=
    -bor
    一样有效吗?@xjcl你是对的,它没有。如果文件已隐藏,则存在问题。我不认为没有
    -force
    gci
    可以返回隐藏文件,但人们永远不知道。为了安全起见,我已经确定了答案。