首页 > 解决方案 > 编辑注册表时使用 Push-Location/Pop-Location 有用吗?

问题描述

这是我自己的带有 Push/Pop 行的脚本。从这里得到 PushPop 的想法。他们被评论了,因为他们显然什么都不做……在功能上很糟糕。

# DisableCortana.ps1
 
$newKeyLocation="HKLM:\SOFTWARE\Policies\Microsoft\Windows"
$newKeyName="Windows Search"
 
#Push-Location       # "Adds that location at the top of the stack" - for what?
# I guess if your location is changed mid-thread execution, this could make sense, but how could that be allowed in the first place?
Set-Location $newKeyLocation
 
if(Test-Path "$newKeyLocation\$newKeyname"){
    echo "Path exists, aborting"
 
}else{
 
New-Item -Path "$newKeyLocation" -Name "$newKeyName"
 
$newKey="HKLM:\SOFTWARE\Policies\Microsoft\Windows\Windows Search"
$newValueName="AllowCortana"
$allowCortanaValue="0"
 
New-ItemProperty -Path $newKey -Name $newValueName -Value $allowCortanaValue `
-PropertyType DWORD -Force | Out-Null
 
#Pop-Location
}

标签: powershellwindows-10registry

解决方案


在您的情况下Push-LocationPop-Location这里是为了保证您的脚本不会修改当前位置。

在一个新的 PowerShell 会话 Test 中:

Get-Location 
$newKeyLocation="HKLM:\SOFTWARE\Policies\Microsoft\Windows"
$a = Set-Location $newKeyLocation 
Get-Location 

看看 prop 发生了变化,你不再是在默认的文件系统提供程序中,而是在注册表提供程序中。

现在进行一个新的 PowerShell 会话和测试:

Get-Location 
Push-Location
$newKeyLocation="HKLM:\SOFTWARE\Policies\Microsoft\Windows"
$a = Set-Location $newKeyLocation 
Pop-Location
Get-Location 

提示停留在文件系统中

我认为您可以查看About Providers 文档。该位置在提供程序的顶部运行set-locationPush-Location并且Pop-Location在全球范围内使用不同的 PSDrive,它们不仅是文件系统,而且可以是注册表、别名(请参阅 get-psprovider、get-psdrive)


推荐阅读