【问题标题】:Toggle "show hidden files and folders with Powershell切换“使用 Powershell 显示隐藏的文件和文件夹
【发布时间】:2021-05-12 20:53:05
【问题描述】:

我一直在尝试在 .bat 或 .ps 脚本中创建一个用于切换“显示隐藏文件/文件夹”选项的行项目。到目前为止我没有运气。我尝试过以各种方式使用get-childitem 命令,但它对我不起作用。我错过了什么?这甚至是可以做到的吗?

【问题讨论】:

  • 这有点不清楚。您是否尝试在 CMD 或 PowerShell 中显示隐藏文件/文件夹,或切换资源管理器的显示隐藏文件/文件夹选项?如果是后者,我建议调查注册表编辑。我不确定是否有 CLI 命令可以更改资源管理器的选项。

标签: powershell file directory hidden


【解决方案1】:

你想在这里实现两件事。

  1. 设置注册表项的值,可以使用Set-ItemProperty 完成。
  2. 刷新所有打开的资源管理器窗口。我们可以使用名为 Shell.Application 的 ComObject 来执行此操作。

注意如果您没有打开任何资源管理器窗口,则只有在资源管理器窗口中按 F5 才会生效。

话虽如此,您可以编写一个类似于下面的小函数来切换资源管理器中的隐藏文件值。

function Show-HiddenFiles {
    [CmdletBinding(DefaultParameterSetName = "On")]
    Param (
        [Parameter(Mandatory = $true, ParameterSetName = "On")]
        [System.Management.Automation.SwitchParameter]
        $On,

        [Parameter(Mandatory = $true, ParameterSetName = "Off")]
        [System.Management.Automation.SwitchParameter]
        $Off
    )
    Process {
        # Set a variable with the value we want to set on the registry value/subkey.
        if ($PSCmdlet.ParameterSetName -eq "On") { $Value = 1 }
        if ($PSCmdlet.ParameterSetName -eq "Off") { $Value = 2 }

        # Define the path to the registry key that contains the registry value/subkey
        $Path = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"
        # Set the registry value/subkey.
        Set-ItemProperty -Path $Path -Name Hidden -Value $Value

        # Refresh open Explorer windows.
        # You will need to refresh the window if you have none currently open.
        # Create the Shell.Application ComObject
        $Shell = New-Object -ComObject Shell.Application
        # For each one of the open windows, refresh it.
        $Shell.Windows() | ForEach-Object { $_.Refresh() }
    }
}

使用简单。

Show-HiddenFiles -On
Show-HiddenFiles -Off

如果您只想在 PowerShell 中显示隐藏文件,您可以使用 -Hidden or -Force parameter of Get-ChildItem

-Hidden 仅返回隐藏文件。
-Force 返回隐藏和非隐藏文件。

【讨论】:

  • 不客气,@Evolver。它是我的并不重要,但你应该投票并接受答案,如果有人帮助你解决了你的问题。这有助于其他用户在遇到类似问题时找到解决方案。请参阅What does it mean when an answer is "accepted"? 了解更多信息。
【解决方案2】:

注册表路径:HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced

名称:隐藏

类型:双字

设置:

1 = 显示

2 = 不显示

【讨论】:

    【解决方案3】:

    从 Ash 的 fine script 开始工作,这里有一点改进。 这允许第三个无参数选项,它只会切换当前设置。

    function Show-HiddenFiles {
     
       Param (
              [Parameter(Mandatory = $False,Position=0)]
                [String] $Setting
       )
    
       # Define the path to the registry key that contains the 
       # registry value/subkey
       $Path = "HKCU:\Software\Microsoft\Windows\CurrentVersion" +
               "\Explorer\Advanced"
    
       #No argument toggle setting..
       If ($Args.Count -eq 0) {
    
         $GIPArgs = @{Path = $Path
                      Name = "Hidden"}
    
         If ((Get-ItemProperty @GIPArgs ).Hidden -eq 1) {
           $Value = 2
         }
         Else {$Value = 1}   
       }
       Else {
       # Set a variable with the value we want to set on the 
       # registry value/subkey.
         If ($Setting -eq "On" ) { $Value = 1 }
         Else                    { $Value = 2 }
       }
       
       # Set the registry value/subkey.
       Set-ItemProperty -Path $Path -Name Hidden -Value $Value
    
       # Refresh open Explorer windows.
       # You will need to refresh the window if you have none 
       # currently open.
       # Create the Shell.Application ComObject
       $Shell = New-Object -ComObject Shell.Application
       # For each one of the open windows, refresh it.
        $Shell.Windows() | ForEach-Object { $_.Refresh() }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-31
      • 2016-01-22
      • 2015-02-15
      • 2022-08-20
      • 2020-06-20
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      相关资源
      最近更新 更多