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

后端 未结 9 867
忘掉有多难
忘掉有多难 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 04:53

    You could use a PowerShell command. Stick this in your batch script and it'll create a shortcut to %~f0 in %userprofile%\Start Menu\Programs\Startup:

    powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('%userprofile%\Start Menu\Programs\Startup\%~n0.lnk');$s.TargetPath='%~f0';$s.Save()"
    

    If you prefer not to use PowerShell, you could use mklink to make a symbolic link. Syntax:

    mklink saveShortcutAs targetOfShortcut
    

    See mklink /? in a console window for full syntax, and this web page for further information.

    In your batch script, do:

    mklink "%userprofile%\Start Menu\Programs\Startup\%~nx0" "%~f0"
    

    The shortcut created isn't a traditional .lnk file, but it should work the same nevertheless. Be advised that this will only work if the .bat file is run from the same drive as your startup folder. Also, apparently admin rights are required to create symbolic links.

提交回复
热议问题