creating a shortcut for a exe from a batch file

后端 未结 9 1525
清酒与你
清酒与你 2020-11-27 05:39

how to create a shortcut for a exe from a batch file.

i tried

call link.bat \"c:\\program Files\\App1\\program1.exe\" \"C:\\Documents and Settings\\         


        
9条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 06:29

    This is the kind of thing that PowerShell is really good at, and is therefore a reason to eschew batch files and get on PowerShell the bandwagon.

    PowerShell can talk to .NET. For example, you can get the location of the Desktop like this:

    [Environment]::GetFolderPath("Desktop")
    

    PowerShell can talk to COM objects, including WScript.Shell, which can create shortcuts:

    New-Object -ComObject WScript.Shell).CreateShortcut( ... )
    

    So your script might look like:

    $linkPath = Join-Path ([Environment]::GetFolderPath("Desktop")) "MyShortcut.lnk"
    $targetPath = Join-Path ([Environment]::GetFolderPath("ProgramFiles")) "MyCompany\MyProgram.exe"
    $link = (New-Object -ComObject WScript.Shell).CreateShortcut( $linkpath )
    $link.TargetPath = $targetPath
    $link.Save()
    

    Shortcuts have a lot of settings that WScript.Shell can't manipulate, like the "run as administrator" option. These are only accessible through the Win32 interface IShellLinkDataList, which is a real pain to use, but it can be done.

提交回复
热议问题