简体   繁体   English

切换“使用 Powershell 显示隐藏文件和文件夹

[英]Toggle "show hidden files and folders with Powershell

I have been trying to create a line item within a.bat or.ps script that toggles the "Show hidden files/folders" option ON.我一直在尝试在 a.bat 或 .ps 脚本中创建一个用于切换“显示隐藏文件/文件夹”选项的行项目。 I am having no luck so far.到目前为止我没有运气。 I have tried using the get-childitem command in various flavors and it's not working for me.我尝试过以各种方式使用get-childitem命令,但它对我不起作用。 What am I missing?我错过了什么? Is this even something that can be done?这甚至是可以做到的吗?

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

Name: Hidden名称:隐藏

Type: DWord类型:双字

Settings:设置:

1 = Show 1 = 显示

2 = Don't Show 2 = 不显示

There are two things you want to achieve here.您想在这里实现两件事。

  1. Set the value of a registry key which can be done with Set-ItemProperty .设置可以使用Set-ItemProperty完成的注册表项的值。
  2. Refresh any open Explorer windows.刷新任何打开的资源管理器 windows。 We can use a ComObject called Shell .我们可以使用一个名为Shell的 ComObject。 Application to do this. 申请执行此操作。

NB This will not take effect until you press F5 in an Explorer Window if you do not have any Explorer Windows open.注意:如果您没有打开任何 Explorer Windows,则在您在 Explorer Window 中按 F5 之前,这不会生效。

That being said, you can write a small function much like the one below that will toggle the Hidden Files value in Explorer.话虽如此,您可以编写一个小的 function,就像下面的那样,它将在资源管理器中切换隐藏文件的值。

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() }
    }
}

Usage is simple.用法很简单。

Show-HiddenFiles -On
Show-HiddenFiles -Off

If you just want to display hidden files in PowerShell you can use either the -Hidden or -Force parameter of Get-ChildItem如果您只想在 PowerShell 中显示隐藏文件,您可以使用Get-ChildItem 的 -Hidden 或 -Force 参数

-Hidden returns only the Hidden files. -Hidden仅返回隐藏文件。
-Force returns Hidden and non-Hidden files. -Force返回隐藏和非隐藏文件。

Working from Ash's fine script ,here's a slight enhancement.使用 Ash 的精美脚本,这里有一点改进。 This allows a third option of no parameter which will just toggles the current setting.这允许第三个无参数选项,它只会切换当前设置。

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() }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM