37

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

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

4

4 回答 4

44
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

于 2009-02-10T21:59:28.513 回答
25

以下是我用于处理 .lnk 文件的函数。它们是@Nathan Hartley 提到的此处找到的函数的修改版本。我改进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()
  }
}
于 2014-02-23T11:24:54.557 回答
3

我不认为有本土的方式。

有这个 DOS 工具:Shortcut.exe

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

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

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

于 2009-01-28T03:20:41.463 回答
3

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

要查看可用属性,您可以$shortcutPS$shortcut = $shell.CreateShortcut($destination)中运行。这将打印所有属性及其当前值。

于 2018-12-06T12:59:50.243 回答