2017-07-06 36 views
0

我正在編寫一些PowerShell腳本來檢查筆記本電腦上是否安裝了記事本++。雖然我在這方面遇到了一些問題。如何檢查Notepad ++是否使用Powershell安裝

下面的代碼:

# 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." 
} 

我手動卸載記事本+ +。然後我執行腳本,顯示的輸出消息是記事本++被安裝,當它不是。爲什麼是這樣?

任何幫助將不勝感激。

回答

0

您從不測試註冊表路徑是否存在。

If($regkey){}總是返回True,因爲變量不爲空,所以始終安裝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." 
} 
+0

您好,我剛剛重新安裝記事本+ +和執行你的建議腳本,它仍然打印出不正確的輸出信息,在這種情況下是它沒有安裝,當它。 –

+0

現在,記事本++被安裝在我的機器上,但是當我運行腳本時,輸出顯示爲未安裝。 –

+0

你確定你的Notepad ++存儲在Wow6432Node中嗎?它存儲在它,沒有問題,它說「已安裝」 – Manu

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." 
} 
+0

不錯的腳本+1,但是有一個常見的誤解,'$ w32.DisplayName'的輸出在這裏'Notepad ++(64-bit x64)'Wow64表示Windows在Windows64上所以32Node是32位子系統。 – LotPings

相關問題