如何检查是否使用Powershell安装了记事本++



我正在编写一些Powershell脚本来检查我的笔记本电脑上是否安装了记事本++。虽然我对此有一些问题。

代码如下:

# Variable(s)
$regkey = "HKEY_LOCAL_MACHINESOFTWAREWow6432NodeNotepad++"
#
# 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++ 已安装,而实际上并非如此。这是为什么呢?

任何帮助将不胜感激。

$w64=Get-ItemProperty HKLM:SoftwareWow6432NodeMicrosoftWindowsCurrentVersionUninstall* | where-Object DisplayName -like 'NotePad++*'
$w32=Get-ItemProperty HKLM:SoftwareMicrosoftWindowsCurrentVersionUninstall*  | 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."
} 

从不测试注册表路径是否存在。

If($regkey){}始终返回True,因为变量不为 null,因此始终安装 Notepad++。

试试这个,它会检查注册表路径是否存在:

if(Test-Path "hklm:SOFTWAREWow6432NodeNotepad++"){
Write-output "Notepad++ is already installed on your machine."
}
Else{
Write-Output "Notepad++ is not installed on your machine."
} 

最新更新