0

I would like to test if notepad++ is installd via powershell and if installed, I will open a text file with notepad++ otherwise with notepad.

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

How can I achieve this? Thanks

pencilCake
  • 51,323
  • 85
  • 226
  • 363

3 Answers3

1

Have a read of this post which I used to come up with the below.

# 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
}
G42
  • 9,791
  • 2
  • 19
  • 34
0

Equivalent of *Nix 'which' command in Powershell?

This should help you. In UNIX, the which command shows you where a program is installed. From there, you can check if returned string is empty or not. Good luck :)

Community
  • 1
  • 1
VirginieLGB
  • 548
  • 2
  • 15
  • 1
    I am not sure this is what OP is looking for. Get-Command finds notepad.exe because it exists in a path in the environment variable %PATH%. This won't work for Notepad++.exe because is not (by default) installed in any path within the %PATH% env var. – Bin Mar 23 '16 at 14:40
0

If you can assume default paths you can just check for the .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'
}
Bin
  • 211
  • 1
  • 7