【问题标题】:Editing shortcut (.lnk) properties with Powershell使用 Powershell 编辑快捷方式 (.lnk) 属性
【发布时间】:2023-04-07 09:37:01
【问题描述】:

我找到了一种讨厌的 VBS 方法来执行此操作,但我正在寻找一种本地 PoSh 程序来编辑 .LNK 文件的属性。目标是联系远程机器,复制具有大部分正确属性的现有快捷方式,并编辑其中的几个。

如果编写一个新的快捷方式文件更容易,那也可以。

【问题讨论】:

    标签: powershell file shortcuts


    【解决方案1】:

    对@JasonMArcher 的回答的简短补充..

    要查看可用属性,您只需在 PS 中的 $shortcut = $shell.CreateShortcut($destination) 之后运行 $shortcut。 这将打印所有属性及其当前值。

    【讨论】:

      【解决方案2】:

      以下是我用来处理 .lnk 文件的函数。它们是@Nathan Hartley 提到的here 函数的修改版本。我改进了 Get-Shortcut 以处理像 * 这样的通配符,方法是将字符串传递给 dir 以将它们扩展为 FileInfo 对象集。

      function Get-Shortcut {
        param(
          $path = $null
        )
      
        $obj = New-Object -ComObject WScript.Shell
      
        if ($path -eq $null) {
          $pathUser = [System.Environment]::GetFolderPath('StartMenu')
          $pathCommon = $obj.SpecialFolders.Item('AllUsersStartMenu')
          $path = dir $pathUser, $pathCommon -Filter *.lnk -Recurse 
        }
        if ($path -is [string]) {
          $path = dir $path -Filter *.lnk
        }
        $path | ForEach-Object { 
          if ($_ -is [string]) {
            $_ = dir $_ -Filter *.lnk
          }
          if ($_) {
            $link = $obj.CreateShortcut($_.FullName)
      
            $info = @{}
            $info.Hotkey = $link.Hotkey
            $info.TargetPath = $link.TargetPath
            $info.LinkPath = $link.FullName
            $info.Arguments = $link.Arguments
            $info.Target = try {Split-Path $info.TargetPath -Leaf } catch { 'n/a'}
            $info.Link = try { Split-Path $info.LinkPath -Leaf } catch { 'n/a'}
            $info.WindowStyle = $link.WindowStyle
            $info.IconLocation = $link.IconLocation
      
            New-Object PSObject -Property $info
          }
        }
      }
      
      function Set-Shortcut {
        param(
        [Parameter(ValueFromPipelineByPropertyName=$true)]
        $LinkPath,
        $Hotkey,
        $IconLocation,
        $Arguments,
        $TargetPath
        )
        begin {
          $shell = New-Object -ComObject WScript.Shell
        }
      
        process {
          $link = $shell.CreateShortcut($LinkPath)
      
          $PSCmdlet.MyInvocation.BoundParameters.GetEnumerator() |
            Where-Object { $_.key -ne 'LinkPath' } |
            ForEach-Object { $link.$($_.key) = $_.value }
          $link.Save()
        }
      }
      

      【讨论】:

      • 我只是想让您知道这些功能非常棒,感谢您发布它们。他们只是在 Robocopy 工作中为我省去了很多麻烦。
      • 如果您获得了 .TargetPath 之类的空白属性,请注意 CreateShortcut() 需要 .LNK 文件的完整 UNC 路径。它会忽略当前目录并且“.\myshortcut.lnk”也会失败。您不会收到错误,只是空属性。
      【解决方案3】:
      Copy-Item $sourcepath $destination  ## Get the lnk we want to use as a template
      $shell = New-Object -COM WScript.Shell
      $shortcut = $shell.CreateShortcut($destination)  ## Open the lnk
      $shortcut.TargetPath = "C:\path\to\new\exe.exe"  ## Make changes
      $shortcut.Description = "Our new link"  ## This is the "Comment" field
      $shortcut.Save()  ## Save
      

      在这里找到了 VB 版本的代码: http://www.tutorialized.com/view/tutorial/Extract-the-target-file-from-a-shortcut-file-.lnk/18349

      【讨论】:

      • 仅供参考。此解决方案适用于本地文件,但不适用于 UNC 路径。
      • 我做了一些测试,这适用于 UNC 路径。我不确定@iraSenthil 测试了什么。
      • 我不确定为什么该代码不适用于 UNC 路径,多年来我在 VB-Script/JScript 中做了同样的事情。只需获得权限和访问权限即可进行更改(请参阅 Marco Shaws 发布的注释中的示例路径)。至于 Powershell,Tobias 把所有东西都放在了两个不错的函数中,get-shortcut 和 set-shortcut。在这里找到:powershell.com/cs/media/p/7895.aspx
      • 为我工作出色。另一个有用的属性是 $shortcut.Arguments 和 $shortcut.WorkingDirectory。也适用于 UNC 路径
      • 这里有一些文档,其中包含每个正在使用的属性的示例:computerperformance.co.uk/powershell/…
      【解决方案4】:

      我认为没有原生方式。

      有这个 DOS 工具:Shortcut.exe

      您仍然需要将 util 复制到远程系统,然后可能使用 WMI 调用它以进行您要查找的更改。

      我认为更简单的方法是覆盖和/或创建一个新文件。

      您是否可以通过远程共享访问这些系统?

      【讨论】:

      • 嗨,马可。当然,我可以通过远程共享访问它们。
      • 好吧,像这样的常规管理工作我做的不够多,但我想你可以简单地使用共享来远程更新 lnk 文件。
      • 远程,对于 95/98/XP/2003 和"\\$Computer\C$\ProgramData\Microsoft\Windows\Start Menu\Programs\The Shortcut.lnk" 适用于 Vista/Win 7/2008。
      猜你喜欢
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 1970-01-01
      • 2013-08-04
      • 2016-07-27
      • 1970-01-01
      • 1970-01-01
      • 2014-05-12
      相关资源
      最近更新 更多