如何测试 Notepad++ 是不是通过 powershell 安装?

Posted

技术标签:

【中文标题】如何测试 Notepad++ 是不是通过 powershell 安装?【英文标题】:How can I test if Notepad++ is installed via powershell?如何测试 Notepad++ 是否通过 powershell 安装? 【发布时间】:2016-07-10 12:20:41 【问题描述】:

我想测试notepad++是否通过powershell安装,如果安装了,我会用notepad++打开一个文本文件,否则用notepad。

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

 notepad++ $textfile

notepad $textfile

我怎样才能做到这一点? 谢谢

【问题讨论】:

blogs.technet.microsoft.com/heyscriptingguy/2013/11/15/… ? 【参考方案1】:

Equivalent of *Nix 'which' command in Powershell?

这应该对您有所帮助。在 UNIX 中,which 命令显示程序的安装位置。 从那里,您可以检查返回的字符串是否为空。 祝你好运:)

【讨论】:

我不确定这是 OP 正在寻找的。 Get-Command 找到 notepad.exe,因为它存在于环境变量 %PATH% 的路径中。这对 Notepad++.exe 不起作用,因为它没有(默认情况下)安装在 %PATH% 环境变量内的任何路径中。【参考方案2】:

阅读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

【讨论】:

【参考方案3】:

如果您可以假设默认路径,则只需检查 .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'

【讨论】:

使用$env:ProgramFiles 比猜测文件夹名称要好。

以上是关于如何测试 Notepad++ 是不是通过 powershell 安装?的主要内容,如果未能解决你的问题,请参考以下文章

pow(x, 0.5f) 的快速实现是不是比快速 sqrt(x) 快?

如何将 Notepad++ 配置为使用空格而不是制表符?

Notepad++ 、editplus中如何对 HTML/CSS/JS 格式化?

Notepad++:为现有命令添加新快捷方式

通过正则表达式在 Notepad++ 中替换时如何使用条件

Notepad++如何关闭最近打开的文件的历史记录功能