mbo*_*007 5 registry powershell
我正在尝试找到一种使用 PowerShell 在默认文本编辑器中打开非 txt文件(在本例中为主机文件)的方法。
看到这篇 Reddit 帖子后,我取得了一些进展,但$txt_editor结果总是返回 Notepad.exe,即使 Notepad++ 是我的 txt 文件的默认编辑器。
$hosts_file = "$env:windir\System32\drivers\etc\hosts"
$txt_editor = ((Get-ItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\txtfile\shell\open\command').'(Default)').trimend(" %1")
Start-Process -FilePath $txt_editor -Verb Runas -ArgumentList $hosts_file
Run Code Online (Sandbox Code Playgroud)
这也会返回 Notepad.exe:
(Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\OpenWithList' -Name a).a
Run Code Online (Sandbox Code Playgroud)
如果我在注册表编辑器中查看上述位置,我确实会看到 Notepad++ 列出了 key d,但我不知道如何仅通过查看注册表项来判断默认文本编辑器是什么,因为我在中看到的两个解决方案Reddit 不起作用。
我使用的是 Windows 10,我正在寻找的解决方案将返回实际的默认文本编辑器文件位置,以便它可用于打开文件,如上所示。
这个问题仍然没有收到我实际问题的答案,因为唯一的答案没有显示如何使用配置的默认文本文件编辑器打开非 txt文件。
更新:我现在尝试在 Windows 11 上复制此内容,但注册表项HKEY_CLASSES_ROOT\txtfile\shell\open\command不存在。如果涉及注册表,我需要一个解决方案,该解决方案也将包括这方面的解决方案。我将 Notepad++ 配置为默认值,并在 处创建了一个密钥HKEY_CLASSES_ROOT\txtfilelegacy\shell\printto\command。
Fox*_*loy 11
Start 命令(它是 的别名Start-Process)将在其默认编辑器中启动任何文件。
start .\MyCoolbmp.bmp
#Opens in MSPaint
start .\SomeNotes.txt
#Opens in Notepad
start .\SomeJason.json
#Opens in Visual Studio, go ahead and grab a coffee...
Run Code Online (Sandbox Code Playgroud)
如果我不得不猜测为什么你的不起作用,那就是你提供的注册表项是用于系统的注册表项,而从 Windows 7 及更高版本开始,用户的默认编辑器存储在 HKEY_CURRENT_USER 配置单元内,位于此路径Windows\CurrentVersion\Explorer\FileExts\.txt\UserChoice'。
以下是相关值:
$txtKey = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\UserChoice"
Get-ItemProperty -Path $txtKey | tee-object -variable txtPath
Hash : noCJnt8yX5Y=
ProgId : VSCode.txt
Run Code Online (Sandbox Code Playgroud)
这与 HKCR:\Applications 中找到的信息相关,可以在其中找到真实路径。
get-itemproperty Registry::\HKEY_CLASSES_ROOT\$($txtPath.ProgId)\shell\open\command
(default) : "C:\Program Files\Microsoft VS Code\Code.exe" "%1"
#...
Run Code Online (Sandbox Code Playgroud)
如果您获取该(默认)值,现在您就获得了与文本文件关联的编辑器的真实路径。
要了解有关该主题的更多信息,这篇博文非常好,详细介绍了关联的工作原理。
使用@FoxDeploy 的回答中的信息,我能够构建一个有效的 PowerShell 函数。
function Edit-HostsFile {
# Self-elevate if required
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
$hosts_file = "$env:windir\System32\drivers\etc\hosts"
# Get the configured application default for editing .txt files.
$txt_app = ((Get-ItemProperty -Path Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\UserChoice -Name ProgId).ProgId)
$txt_editor = ((Get-ItemProperty -Path Registry::HKEY_CLASSES_ROOT\$($txt_app)\shell\open\command).'(Default)').replace(' "%1"', '')
# Open the hosts file in the editor.
Start-Process -FilePath $txt_editor -Verb Runas -ArgumentList $hosts_file
}
}
}
Run Code Online (Sandbox Code Playgroud)
几年前我尝试时,我还没有使用答案中的信息得到有效的解决方案,但在再次尝试后我能够找到答案。包含代码的更完整的解决方案会很有帮助,但创建起来并不需要太长时间。
| 归档时间: |
|
| 查看次数: |
12400 次 |
| 最近记录: |