当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 隐藏任务栏搜索

作者:精品下载站 日期:2024-12-14 08:03:29 浏览:15 分类:玩电脑

使用 PowerShell 隐藏任务栏搜索


昨天我分享了一些用于将 Windows 10 任务栏配置为自动隐藏的 PowerShell 函数。当录制我的 Pluralsight 课程时,这在我的虚拟桌面中非常有用。但即使隐藏起来,我仍然会从搜索框中得到一条恼人的白色条子。所以我也摆脱了它。以下是一些可隐藏和取消隐藏 Windows 10 桌面中的搜索框的 PowerShell 函数。是的,有手动步骤来隐藏此功能,但我在这里自动化!与往常一样,即使您认为不需要这样做,代码中也可能有有用的 PowerShell 块。

控制搜索框的设置位于当前用户的注册表中。注册表设置为 HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Search。在其下方,一个名为 SearchBoxTaskbarMode 的键控制显示。您的桌面上很可能不存在此键。如果双字值为 0,则搜索框将被隐藏。值为 2 将显示它。您可以使用 PowerShell 来设置该值。

Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Search -Name SearchBoxTaskbarMode -Value 0 -Type DWord -Force

如果该条目不存在,-Force 参数将创建该条目。与自动隐藏任务栏一样,您需要重新启动资源管理器进程。因为我似乎经常这样做,所以我决定将其放入一个单独的函数中。原因之一是,使用 Windows Sandbox 时,资源管理器有时不会按应有的方式自动重新启动。我的函数杀死资源管理器,测试它是否正在运行,如果没有则启动它。

Function Restart-Explorer {
    <#
    .Synopsis
    Restart the Windows Explorer process.
    #>
    [cmdletbinding(SupportsShouldProcess)]
    [Outputtype("None")]
    Param()

    Write-Verbose "[$((Get-Date).TimeofDay) BEGIN  ] Starting $($myinvocation.mycommand)"
    Write-Verbose "[$((Get-Date).TimeofDay) BEGIN  ] Stopping Explorer.exe process"
    Get-Process -Name Explorer | Stop-Process -Force
    #give the process time to start
    Start-Sleep -Seconds 2
    Try {
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN  ] Verifying Explorer restarted"
        $p = Get-Process -Name Explorer -ErrorAction stop
    }
    Catch {
        Write-Warning "Manually restarting Explorer"
        Try {
            Start-Process explorer.exe
        }
        Catch {
            #this should never be called
            Throw $_
        }
    }
    Write-Verbose "[$((Get-Date).TimeofDay) END    ] Ending $($myinvocation.mycommand)"
}

然后,我构建了相对简单的函数来禁用和启用搜索框。

Function Disable-TaskBarSearch {
    <#
    .Synopsis
     Disable the Windows taskbar search box
    #>
    [cmdletbinding(SupportsShouldProcess)]
    [Alias("Hide-SearchBar")]
    [OutputType("Boolean")]
    Param()

    Begin {
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN  ] Starting $($myinvocation.mycommand)"
    } #begin
    Process {
        Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Hiding Task Bar Search"

        Try {
            $splat = @{
                Path        = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Search'
                Name        = 'SearchBoxTaskbarMode'
                Value       = 0
                Type        = 'DWord'
                Force       = $True
                ErrorAction = 'Stop'
            }
            Set-ItemProperty @splat
            if ($WhatIfPreference) {
                #return false if using -Whatif
                $False
            }
            else {
                $True
            }
        }
        Catch {
            $False
            Throw $_
        }
        Restart-Explorer
    } #process
    End {
        Write-Verbose "[$((Get-Date).TimeofDay) END    ] Ending $($myinvocation.mycommand)"
    } #end
}

Function Enable-TaskBarSearch {
    <#
    .Synopsis
     Enable the Windows taskbar search box
    #>
    [cmdletbinding(SupportsShouldProcess)]
    [Alias("Show-SearchBar")]
    [OutputType("Boolean")]
    Param()

    Begin {
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN  ] Starting $($myinvocation.mycommand)"
    } #begin
    Process {
        Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Showing Task Bar Search"

        Try {
            $splat = @{
                Path        = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Search'
                Name        = 'SearchBoxTaskbarMode'
                Value       = 2
                Type        = 'DWord'
                Force       = $True
                ErrorAction = 'Stop'
            }
            Set-ItemProperty @splat
            if ($WhatIfPreference) {
                #return false if using -Whatif
                $False
            }
            else {
                $True
            }
        }
        Catch {
            $False
            Throw $_
        }

        Restart-Explorer
    } #process
    End {
        Write-Verbose "[$((Get-Date).TimeofDay) END    ] Ending $($myinvocation.mycommand)"
    } #end
}

可以看到函数调用了Restart-Explorer函数。我的 SearchBarTools.ps1 版本包含该功能。我还决定让该函数将布尔结果写入管道。如果我想验证该过程,我可以在控制脚本中使用它。

这些命令还支持 -WhatIf,它将传递给 Restart-Explorer 函数。

[玩转系统] 使用 PowerShell 隐藏任务栏搜索

现在,我拥有一组 PowerShell 工具,可以将它们与桌面自动化工具一起使用,以完全按照我的课程作业需要配置设置。

希望您在代码中找到有用的东西。享受。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯