无法让此PowerShell脚本递归地从所有文件中删除隐藏文件属性

问题描述 投票:0回答:1

我正在尝试制作一个PowerShell脚本来遍历所有子目录并删除隐藏文件属性,但由于某种原因它不起作用。

$param1 = $args[0]

# Check if the directory exists
if (Test-Path -Path $param1 -PathType Container) {
    # Remove hidden attribute from all files in the directory and its subdirectories
    Get-ChildItem -Path $param1 -File -Recurse | ForEach-Object {
        if ($_.Attributes -band [System.IO.FileAttributes]::Hidden) {
            $_.Attributes = $_.Attributes -band (-bnot [System.IO.FileAttributes]::Hidden)
        }
    }

    # Remove hidden attribute from all directories in the directory and its subdirectories
    Get-ChildItem -Path $param1 -Directory -Recurse | ForEach-Object {
        if ($_.Attributes -band [System.IO.FileAttributes]::Hidden) {
            $_.Attributes = $_.Attributes -band (-bnot [System.IO.FileAttributes]::Hidden)
        }
    }
} else {
    Write-Host "Directory $param1 does not exist."
}

这对我来说在

cmd
中很容易做到,所以我很惊讶这被证明是多么困难。

powershell attributes
1个回答
0
投票
  • 要在枚举时

    Get-ChildItem
    包含隐藏,必须通过
    -Force
    开关。

  • 但是,在当前的情况下,您最好调用特定用途的标准

    attrib.exe
    实用程序,它提供了更简单的解决方案:

    attrib -H /D /S $param1
    
© www.soinside.com 2019 - 2024. All rights reserved.