2016-03-23 33 views
0

我想測試記事本++是否通過powershell安裝,如果安裝,我會打開一個文本文件與記事本++,否則用記事本。如何測試Notepad ++是否通過PowerShell安裝?

$textfile = "d:\fooBar.txt)" 
if (<# notepad++ installed?#> 
{ 
notepad++ $textfile 
} 
notepad $textfile 

我該如何做到這一點? 感謝

+0

https://blogs.technet.microsoft.com/heyscriptingguy/2013/11/15/use-powershell-to-find-installed-software/? –

回答

0

Equivalent of *Nix 'which' command in Powershell?

這會幫助你。在UNIX中,哪個命令將顯示您安裝程序的位置。 從那裏,你可以檢查返回的字符串是否爲空。 祝你好運:)

+1

我不確定這是什麼OP正在尋找。 Get-Command查找notepad.exe,因爲它存在於環境變量%PATH%的路徑中。這不適用於Notepad ++。exe,因爲它不是(默認情況下)安裝在%PATH%env var的任何路徑中。 – Bin

1

有我讀過this post,我曾經拿出下面。

# Get the Notepad++ registry item, if it exists (32bit) 
$np = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | 
Where-Object {$_.DisplayName -eq 'Notepad++'} | 
Select-Object DisplayName,DisplayIcon 

if($np -ne $null){ 
    # Launch based on DisplayIcon being notepad++.exe 
    # You could manipulate this string or another registry entry for added robustness. 
    & $np.DisplayIcon $textfile 
} 
0

如果您可以假定默認路徑,您可以檢查.exe。

if ($ENV:PROCESSOR_ARCHITECTURE -eq 'AMD64'){ 
    $npp = "C:\Program Files (x86)\Notepad++\notepad++.exe" 
} else { 
    $npp = "C:\Program Files\Notepad++\notepad++.exe" 
} 

if ($npp){ 
    &$npp 'c:\folder\document.txt' 
} 
+0

使用'$ env:ProgramFiles'會比猜測文件夾名稱更好。 –

相關問題