使用 PowerShell 管理 Windows 10 任务栏
当我从事 Pluralsight 课程时,我倾向于设置虚拟机进行录制。不过,最近我一直在尝试使用 Windows 10 Sandbox。当我只需要 Windows 10 桌面时,这非常方便。当我设置系统时,我需要配置特定的设置。当然,我使用 PowerShell 脚本来自动化该过程。我想要解决的一项问题是 Windows 10 任务栏。当我录制课程时,我喜欢让它自动隐藏。当然,我可以手动设置它。但这并不有趣。
经过一些在线研究后,我发现了这个页面。除了手动步骤之外,作者还提供了一段PowerShell代码!我假设会有我可以配置的注册表设置,并希望找到它。但这更好。作者编写的代码是为了在 CMD 提示符下调用一些 PowerShell 命令而编写的。但由于我已经在桌面配置中使用 PowerShell,因此我采用了他的代码并创建了可重用的 PowerShell 函数。
黑客的方法是修改“HKCU:SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\StuckRects3”中的注册表项。设置密钥是二进制的。但在 PowerShell 中,它将显示为数组。
要切换任务栏的自动隐藏设置,您只需设置项目 [8]。值为 3 将启用自动隐藏,值为 2 将关闭它。有一个警告,那就是您需要重新启动资源管理器进程才能使更改生效。没问题。我可以使用停止进程。想看看我想出了什么吗?
#requires -version 5.1
# https://linux-console.net/677619/how-to-hide-the-taskbar-on-windows-10/
Function Enable-AutoHideTaskBar {
#This will configure the Windows taskbar to auto-hide
[cmdletbinding(SupportsShouldProcess)]
[Alias("Hide-TaskBar")]
[OutputType("None")]
Param()
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
$RegPath = 'HKCU:SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\StuckRects3'
} #begin
Process {
if (Test-Path $regpath) {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Auto Hiding Windows 10 TaskBar"
$RegValues = (Get-ItemProperty -Path $RegPath).Settings
$RegValues[8] = 3
Set-ItemProperty -Path $RegPath -Name Settings -Value $RegValues
if ($PSCmdlet.ShouldProcess("Explorer", "Restart")) {
#Kill the Explorer process to force the change
Stop-Process -Name explorer -Force
}
}
else {
Write-Warning "Can't find registry location $regpath."
}
} #process
End {
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
} #end
}
Function Disable-AutoHideTaskBar {
#This will disable the Windows taskbar auto-hide setting
[cmdletbinding(SupportsShouldProcess)]
[Alias("Show-TaskBar")]
[OutputType("None")]
Param()
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
$RegPath = 'HKCU:SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\StuckRects3'
} #begin
Process {
if (Test-Path $regpath) {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Auto Hiding Windows 10 TaskBar"
$RegValues = (Get-ItemProperty -Path $RegPath).Settings
$RegValues[8] = 2
Set-ItemProperty -Path $RegPath -Name Settings -Value $RegValues
if ($PSCmdlet.ShouldProcess("Explorer", "Restart")) {
#Kill the Explorer process to force the change
Stop-Process -Name explorer -Force
}
}
else {
Write-Warning "Can't find registry location $regpath."
}
} #process
End {
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
} #end
}
因为我正在更改注册表并终止进程,所以这些函数支持 -WhatIf。 Set-ItemProperty 将继承 WhatIf 首选项。从技术上讲,Stop-Process 也是如此,但我想演示如何为无法识别 -WhatIf 的命令添加自己的 WhatIf 处理。
该命令被编写为在本地工作,并且仅影响当前用户。这些函数还具有更清晰且特定于任务的别名。
您可能不需要这些命令,但我希望有一种技术或概念对您有用。
现在请原谅,我必须录制这门新课程。

