2016-01-29 140 views
0

我的快捷方式經常被打破,因爲我移動了一些東西,不幸的是,Windows鏈接跟蹤器無法跟上。有沒有辦法以編程方式(使用Powershell)讀取和編輯快捷方式的屬性?我想要運行一個腳本來搜索整個硬盤驅動器(或者我指定的任何位置)的文件,以匹配目標名稱,然後使用該新位置更新快捷方式,假設它是正確的文件。以編程方式編輯Windows快捷方式

+0

有此一對夫婦的組件來使用它,但是有可能。使用shorcut來自http://stackoverflow.com/questions/9701840/how-to-create-a-shortcut-using-powershell。您也可以使用該邏輯來編輯它們。嘗試一下,如果你卡住回來並更新你的問題。 – Matt

回答

-1

這裏是創建快捷方式的函數。你可以研究它是如何工作在你的情況

https://github.com/gangstanthony/PowerShell/blob/master/Create-Shortcut.ps1

# Create-Shortcut 
# 
# Create-Shortcut -Source C:\temp\test.txt -DestinationLnk C:\temp\test.txt.lnk 
# 
# Arguments 
# Description 
# FullName 
# Hotkey 
# IconLocation = '%SystemRoot%\system32\SHELL32.dll,16' # printer 
# RelativePath 
# TargetPath 
# WindowStyle 
# WorkingDirectory 

function Create-Shortcut { 
    param (
     [string]$Source, 
     [string]$DestinationLnk, 
     [string]$Arguments 
    ) 

    BEGIN { 
     $WshShell = New-Object -ComObject WScript.Shell 
    } 

    PROCESS { 
     if (!$Source) {Throw 'No Source'} 
     if (!$DestinationLnk) {Throw 'No DestinationLnk'} 

     $Shortcut = $WshShell.CreateShortcut($DestinationLnk) 
     $Shortcut.TargetPath = $Source 
     if ($Arguments) { 
      $Shortcut.Arguments = $Arguments 
     } 
     $Shortcut.Save() 
    } 

    END { 
     function Release-Ref ($ref) { 
      ([System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$ref) -gt 0) 
      [System.GC]::Collect() 
      [System.GC]::WaitForPendingFinalizers() 
     } 
     $Shortcut, $WshShell | % {$null = Release-Ref $_} 
    } 
} 
+0

此答案僅用於鏈接,可能會被標記。 – Matt

+0

'$ sh = New-Object -COM WScript.Shell; $ targetPath = $ sh.CreateShortcut('C:\ Full \ Path \ To \ test.lnk')。TargetPath' –