How do I create a shortcut via command-line in Windows?

后端 未结 9 903
忘掉有多难
忘掉有多难 2020-11-28 04:17

I want my .bat script (test.bat) to create a shortcut to itself so that I can copy it to my windows 8 Startup folder.

I have written this line of code to copy the f

9条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 05:17

    Rohit Sahu's answer worked best for me in Windows 10. The PowerShell solution ran, but no shortcut appeared. The JScript solution gave me syntax errors. I didn't try mklink, since I didn't want to mess with permissions.

    I wanted the shortcut to appear on the desktop. But I also needed to set the icon, the description, and the working directory. Note that MyApp48.bmp is a 48x48 pixel image. Here's my mod of Rohit's solution:

    @echo off
    cd c:\MyApp
    echo Set oWS = WScript.CreateObject("WScript.Shell") > CreateShortcut.vbs
    echo sLinkFile = "%userprofile%\Desktop\MyApp.lnk" >> CreateShortcut.vbs
    echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs
    echo oLink.TargetPath = "C:\MyApp\MyApp.bat" >> CreateShortcut.vbs
    echo oLink.WorkingDirectory = "C:\MyApp" >> CreateShortcut.vbs
    echo oLink.Description = "My Application" >> CreateShortcut.vbs
    echo oLink.IconLocation = "C:\MyApp\MyApp48.bmp" >> CreateShortcut.vbs
    echo oLink.Save >> CreateShortcut.vbs
    cscript CreateShortcut.vbs
    del CreateShortcut.vbs
    

提交回复
热议问题