Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 file but I haven't yet found a way to create the said shortcut, as you can see it only copies the script.

xcopy "C:\Users\Gabriel\Desktop\test.bat" "C:\Users\Gabriel\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup" 

Can you help me out?

like image 775
Maslor Avatar asked May 04 '15 11:05

Maslor


People also ask

How do I run a shortcut from the command line?

Run the application using a shortcut to cmd /kRight-click where you want the shortcut created and choose New → Shortcut. Type cmd.exe /k string , where string is the command you want to execute, then click Next. Give the shortcut an appropriate name and click Finish.

How do I create a shortcut for cmd in Windows 11?

Here's how. Step 1: Press Windows key + S to open Window Search, type in cmd, and press Enter. Step 2: Type in the command given below and press Enter. Step 3: In the Applications window that appears, right-click on the app and select Create shortcut from the menu.


1 Answers

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.

like image 67
rojo Avatar answered Oct 03 '22 12:10

rojo