60

我想創建PowerShell的快捷方式,用於該可執行文件的快捷方式:如何創建使用PowerShell

C:\Program Files (x86)\ColorPix\ColorPix.exe 

如何才能做到這一點?

+1

開始PowerShell 5.0 New-Item,Remove-Item和Get-ChildItem已得到增強以支持創建和管理符號鏈接[請參閱此答案](http://stackoverflow.com/a/29002672/608772) – JPBlanc 2015-03-12 06:12:41

+1

如果你想運行一個快捷方式作爲管理員,你可以使用[這個答案](http://stackoverflow.com/a/29002207/608772)。 – JPBlanc 2015-03-12 07:30:00

回答

86

我不知道在PowerShell中的任何機cmdlet,但你可以使用COM對象,而不是:

$WshShell = New-Object -comObject WScript.Shell 
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\ColorPix.lnk") 
$Shortcut.TargetPath = "C:\Program Files (x86)\ColorPix\ColorPix.exe" 
$Shortcut.Save() 

您可以創建一個PowerShell腳本保存在你的$ PWD設置shortcut.ps1

param ([string]$SourceExe, [string]$DestinationPath) 

$WshShell = New-Object -comObject WScript.Shell 
$Shortcut = $WshShell.CreateShortcut($DestinationPath) 
$Shortcut.TargetPath = $SourceExe 
$Shortcut.Save() 

,並調用它

Set-ShortCut "C:\Program Files (x86)\ColorPix\ColorPix.exe" "$Home\Desktop\ColorPix.lnk"

'Set the additional parameters for the shortcut 
$Shortcut.Arguments = "/argument=value" 

$ Shortcut.Save():如果你想傳遞參數給目標exe文件,它可以這樣做。

爲方便起見,這裏是set-shortcut.ps1的修改版本。它接受參數作爲其第二個參數。

param ([string]$SourceExe, [string]$ArgumentsToSourceExe, [string]$DestinationPath) 
$WshShell = New-Object -comObject WScript.Shell 
$Shortcut = $WshShell.CreateShortcut($DestinationPath) 
$Shortcut.TargetPath = $SourceExe 
$Shortcut.Arguments = $ArgumentsToSourceExe 
$Shortcut.Save() 
+0

謝謝,它的作品:) – cethint 2012-03-14 12:27:00

+1

高興地幫助,接受這個答案! Thanks1 – 2012-03-14 12:34:59

+1

非常小,但爲了一致性,我將'Set-ShortCut' cmdlet的語法更像'MKLINK'或'Set-Alias',別名或鏈接作爲第一個參數出現,然後目標。 'param([string] $ LinkPath,[string] $ TargetPath)' – orad 2014-01-28 18:51:55

19

開始的PowerShell 5.0 New-ItemRemove-ItemGet-ChildItem已得到增強,以支持創建和管理符號鏈接。 ItemType參數New-Item接受一個新的值SymbolicLink。現在,您可以通過運行New-Item cmdlet在一行中創建符號鏈接。

New-Item -ItemType SymbolicLink -Path "C:\temp" -Name "calc.lnk" -Value "c:\windows\system32\calc.exe" 

要小心一個SymbolicLink是從快捷不同,快捷方式只是一個文件。他們有一個大小(一個小的,只是引用他們指向的地方),他們需要一個應用程序來支持該文件類型才能使用。符號鏈接是文件系統級別,所有內容都將其視爲原始文件。應用程序不需要特殊的支持來使用符號鏈接。

無論如何,如果你想創建一個以管理員身份運行快捷使用PowerShell可以使用

$file="c:\temp\calc.lnk" 
$bytes = [System.IO.File]::ReadAllBytes($file) 
$bytes[0x15] = $bytes[0x15] -bor 0x20 #set byte 21 (0x15) bit 6 (0x20) ON (Use –bor to set RunAsAdministrator option and –bxor to unset) 
[System.IO.File]::WriteAllBytes($file, $bytes) 

如果有人想改變別的東西,你可以參考official Microsoft documentation .lnk文件。

+3

是否可以設置快捷方式的圖標? – orad 2015-08-10 03:59:53

+1

但是,符號鏈接與快捷方式有很大不同。在'「$ {env:AppData} \ Microsoft \ Windows \ SendTo」中使用'New-Item'創建的符號鏈接不會顯示在資源管理器發送到菜單中,例如,並且不允許自定義快捷方式屬性,如圖標或工作目錄。 – brianary 2015-11-16 22:46:54

+0

@布里亞里是完全正確的,我在這裏混淆了!我編輯我的答案,以便任何人都可以downvote。 – JPBlanc 2015-11-17 06:36:10