我正在写一些powershell脚本来检查我的笔记本电脑上是否安装了notepad ++。虽然我对此有一些问题。
以下代码:
# Variable(s)
$regkey = "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Notepad++"
#
# Check if Notepad ++ is already installed.
If($regkey)
{
Write-output "Notepad++ is already installed on your machine."
}
Else
{
Write-Output "Notepad++ is not installed on your machine."
}
我手动卸载了notepad ++。然后我执行了脚本,显示的输出消息是安装了notepad ++,当它不是时。为什么是这样?
非常感谢任何帮助。
答案 0 :(得分:1)
$w64=Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | where-Object DisplayName -like 'NotePad++*'
$w32=Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | where-Object DisplayName -like 'NotePad++*'
if ($w64 -or $w32)
{
Write-output "Notepad++ is already installed on your machine."
}
Else{
Write-Output "Notepad++ is not installed on your machine."
}
答案 1 :(得分:0)
您永远不会测试注册表路径是否存在。
If($regkey){}
始终返回True
,因为该变量不为null,因此始终安装Notepad ++。
试试这个,它会检查是否存在注册表路径:
if(Test-Path "hklm:\SOFTWARE\Wow6432Node\Notepad++"){
Write-output "Notepad++ is already installed on your machine."
}
Else{
Write-Output "Notepad++ is not installed on your machine."
}