AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1617185
Accepted
Ξένη Γήινος
Ξένη Γήινος
Asked: 2021-01-14 01:06:53 +0800 CST2021-01-14 01:06:53 +0800 CST 2021-01-14 01:06:53 +0800 CST

如何将 .lnk 文件固定到 PowerShell 中的任务栏?

  • 772

我在 stackoverflow.com 上看到过很多类似的问题,但这次不同,在您想知道之前,我为什么不在 stackoverflow.com 上发布问题?因为我目前被禁止在那里提问,而且我目前没有足够的时间来编辑所有这些问题。

简要说明我想做的事情:我想以编程方式将提升的 cmd、powershell、pwsh、python 等固定到任务栏,当然我知道如何使用 explorer.exe 创建快捷方式并固定它们,但我考虑使用 GUI方法很累很乏味,必须点击多次才能完成一个简单的工作,我想钉很多程序......

我发现了这个:

如何使用 Powershell 创建以管理员身份运行的快捷方式

一个例子:

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\PowerShell.lnk")
$Shortcut.TargetPath = "C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe"
$Shortcut.Save()
$bytes = [System.IO.File]::ReadAllBytes("$Home\Desktop\PowerShell.lnk")
$bytes[0x15] = $bytes[0x15] -bor 0x20
[System.IO.File]::WriteAllBytes("$Home\Desktop\PowerShell.lnk", $bytes)

该示例将在具有管理员权限的桌面上创建一个名为“PowerShell”的默认 PowerShell 快捷方式。

现在,将其包装在一个函数中:

function admin-shortcut {
  PARAM (
    [Parameter(ValueFromPipeline=$true, Mandatory=$true, Position=0)] [system.string] $path,
    [Parameter(ValueFromPipeline=$true, Mandatory=$true, Position=1)] [system.string] $name
    )
    $WshShell = New-Object -comObject WScript.Shell
    $Shortcut = $WshShell.CreateShortcut("$Home\Desktop\$name.lnk")
    $Shortcut.TargetPath = $path
    $Shortcut.Save()
    $bytes = [System.IO.File]::ReadAllBytes("$Home\Desktop\$name.lnk")
    $bytes[0x15] = $bytes[0x15] -bor 0x20
    [System.IO.File]::WriteAllBytes("$Home\Desktop\$name.lnk", $bytes)
}

我在 stackoverflow.com 上看到了很多关于将程序直接固定到任务栏的方法的帖子,但没有一个能解决我的问题,因为我想给固定的快捷方式管理员权限,我见过的方法都没有。

所以我用谷歌搜索了如何使用PowerShell创建管理员快捷方式,找到了上面提到的方法,现在我只需要弄清楚如何将.lnk文件固定到任务栏,不幸的是所有谷歌搜索结果都是“如何将快捷方式固定到任务栏”谈论直接固定程序,它们都不涉及 .lnk 文件,我已经解释了为什么它们在这种情况下不起作用。

那么你有什么想法吗?任何帮助将不胜感激。


不再适用于 Windows 10 的过时方法:

方法#1

$shell = new-object -com "Shell.Application"  
$folder = $shell.Namespace((Join-Path $env:SystemRoot System32\WindowsPowerShell\v1.0))
$item = $folder.Parsename('powershell_ise.exe')
$item.invokeverb('taskbarpin');

方法#2

$shell = new-object -com "Shell.Application"  
$folder = $shell.Namespace('C:\Windows')    
$item = $folder.Parsename('notepad.exe')
$verb = $item.Verbs() | ? {$_.Name -eq 'Pin to Tas&kbar'}
if ($verb) {$verb.DoIt()}

方法#3

$sa = new-object -c shell.application
$pn = $sa.namespace($env:windir).parsename('notepad.exe')
$pn.invokeverb('taskbarpin')

我将所有这些代码粘贴到 PowerShell 中,没有出现错误消息,但是这些命令实际上什么也没做,我什至重新启动了 explorer.exe,仍然没有观察到任何变化......


但不,等等!一切都没有丢失,我可以将生成的快捷方式拖动到任务栏以将其固定到任务栏,我可以右键单击并固定开始,但想象一下拖动 2 打...如果我能弄清楚这些动作到底是做什么的,我可以用命令复制它们......


我已经打开%Appdata%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar

文件夹并发现我固定的一些(非 UWP)应用程序图标确实在那里,

我在这里找到了一些添加到开始菜单的应用程序图标:

%AppData%\Microsoft\Internet Explorer\Quick Launch\User Pinned\StartMenu

但是,我已经确认简单地复制那里的快捷方式是行不通的。

发现这个:https ://docs.microsoft.com/en-us/windows/configuration/configure-windows-10-taskbar ,它很有用。


我找到了一种方法来做到这一点,因为我只想在 xml 文件中添加行,我可以使用它:

[string[]]$xmlconfig=get-content $template | % {
    $_
    if ($_ -match $pattern) {$line}
}

我知道这很愚蠢,这就是为什么我没有将其发布为答案,但它确实完成了工作,不要嘲笑我,因为我还在学习,我目前正在研究如何正确操作 .xml 文件。 ..

我试过了:

[xml]$template=Get-Content $templatefile
$template.SelectNodes("//DesktopApp")

但它不起作用,它也不适用于从 .admx 文件转换的 xml 对象,但是 .ChildNodes 有效,Get-Member 没有显示它......我认为将它视为纯文本文件是最好的现在方法,否则我必须使用这样的东西:

$template.LayoutModificationTemplate.CustomTaskbarLayoutCollection.TaskbarLayout.TaskbarPinList.DesktopApp.DesktopApplicationLinkPath

我已经解决了,我现在将其作为答案发布,我没有追求注册表项,并且我不再想将程序固定到开始菜单,因为我通常不使用它...

windows-10 powershell
  • 2 2 个回答
  • 8218 Views

2 个回答

  • Voted
  1. Best Answer
    Xeнεi Ξэnвϵς
    2021-01-15T04:13:50+08:002021-01-15T04:13:50+08:00

    最低系统要求

    我不知道,我在 Windows 10 20H2 上使用 PowerShell 7.2 preview 2 x64,我已经测试了脚本并确认它可以工作,并发现了一个小错误并修复了它;但是我已经测试了我的代码PowerShell 5.1.19041.610并确认我的代码不能在上面运行,所以也许你至少需要PowerShell Core 6?我真的不知道,但建议使用最新版本的 PowerShell。


    我通过以下方式解决了它:

    1. 另存为pintotaskbar.ps1:
      param(
        [parameter(mandatory=$true)] [validatenotnullorempty()]$execs
      )
      
      if (!(test-path $home\pinnedshortcuts)) {new-item $home\pinnedshortcuts -type "directory" > $null}
      $execs  = $execs -split "\|"
      
      foreach ($exec in $execs) {
        $path = ($exec -split "::")[0]
        $name = ($exec -split "::")[1]
      
        if ($path -notmatch "[C-Zc-z]:(\\[^(<>:`"\/\\|?*\x00-\x1f\x7f)]+)+") {$path=where.exe $path}
      
        $shortcutpath         = "$home\desktop\$name.lnk"
        $wshshell             = new-object -comobject wscript.shell
        $shortcut             = $wshshell.createshortcut($shortcutpath)
        $shortcut.targetpath  = $path
        $shortcut.save()
      
        $bytes                = [system.io.file]::readallbytes($shortcutpath)
        $bytes[0x15]          = $bytes[0x15] -bor 0x20
        [system.io.file]::writeallbytes($shortcutpath,$bytes)
      
        copy-item -path $shortcutpath -destination $home\pinnedshortcuts
      }
      
      $template         = get-content "$psscriptroot\template.xml"
      $pinnedshortcuts  = (get-childitem -path $home\pinnedshortcuts -filter "*.lnk").fullname | %{"`t`t<taskbar:DesktopApp DesktopApplicationLinkPath=`"{0}`" />" -f $_}
      $template         = $template | % {$_;if ($_ -match "<taskbar:taskbarpinlist>") {$pinnedshortcuts}}
      
      $template | out-file  -path "$home\pinnedshortcuts\pinnedshortcuts.xml"
      import-startlayout    -layoutpath $home\pinnedshortcuts\pinnedshortcuts.xml -mountpath c:\
      get-process           -name "explorer" | stop-process & explorer.exe
      

    2. 保存template.xml在同一目录中pintotaskbar.ps1:
      <?xml version="1.0" encoding="utf-8"?>
      <LayoutModificationTemplate
          xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification"
          xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout"
          xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout"
          xmlns:taskbar="http://schemas.microsoft.com/Start/2014/TaskbarLayout"
          Version="1">
        <CustomTaskbarLayoutCollection>
          <defaultlayout:TaskbarLayout>
            <taskbar:TaskbarPinList>
            </taskbar:TaskbarPinList>
          </defaultlayout:TaskbarLayout>
        </CustomTaskbarLayoutCollection>
      </LayoutModificationTemplate>
      

    3. # Accepts a string like:
        # cmd::Command Prompt|pwsh::PowerShell 7|python::Python
      
      .\pintotaskbar.ps1 "cmd::Command Prompt|pwsh::PowerShell 7|python::Python"
      

    我发现了一个奇怪的问题,使用上面的示例将生成一个名为“命令提示符 pwsh”的工作快捷方式,它指向%comspec%,我通过删除 $execs 的类型说明符摆脱了这个[string]问题,它神奇地消失了,我仍然不知道如何它发生了...

    • 3
  2. postanote
    2021-01-14T10:09:07+08:002021-01-14T10:09:07+08:00

    有用于 Win10 的脚本,用于按原样使用任务栏固定,学习形式,根据需要进行调整。

    PinToTaskBar1903.ps1 脚本版本 1

    使用示例 - PIN/UNPIN 区分大小写:

    powershell -noprofile -ExecutionPolicy Bypass -file D:\Scripts\PinToTaskBar1903.ps1 "C:\Windows\Notepad.exe" PIN
    
    powershell -noprofile -ExecutionPolicy Bypass -file C:\Scripts\PinToTaskBar1903.ps1 "C:\Windows\Notepad.exe" UNPIN
    

    更新您的反对意见。

    “脚本链接伪装PEB,我没有看到动词PIN和UNPIN”

    你有没有读过剧本。这个 PIN/UNPIN 直接在脚本的代码中调用出来:

    if (($args[0] -eq "/?") -Or ($args[0] -eq "-h") -Or ($args[0] -eq "--h") -Or ($args[0] -eq "-help") -Or ($args[0] -eq "--help")){
        write-host "This script needs to be run with two arguments."`r`n
        write-host "1 - Full path to the file you wish to pin (surround in quotes)."
        write-host "2 - Either PIN or UNPIN (case insensitive)."
        write-host "Example:-"
        write-host 'powershell -noprofile -ExecutionPolicy Bypass -file PinToTaskBar1903.ps1 "C:\Windows\Notepad.exe" PIN'`r`n
        Break
    }
    
    • 2

相关问题

  • VMware Workstation USB 仲裁服务无法自动启动

  • 如何在域和 Linux 活动目录中启用指纹传感器

  • 资源管理器侧面板中的桌面外壳快捷方式

  • 为什么我不能将文件从 Android 发送到 Windows 10?

  • 在多个文件上打开方式?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve