Creating shortcut with a simple command in CMD.exe - cmd

I need a code for cmd.exe for creating a shortcut on any user's desktop.

If the application will be in the same folder on all machines (like C:\Program Files\Software\Start.exe) then create a shortcut, place it on a network drive, or in the .cmd file's folder, and simply just copy it to C:\%username%\Desktop

Related

How to create a link without .lnk extension?

If I have a file named "C\Test\mypic.jpg", can I create a shortcut with path "C\Test2\mypic.jpg"? Windows always seems to add a ".lnk" suffix, which is unwanted in my case.
Link in this case can mean several things but we can unpack all the possible scenarios:
A shortcut (.lnk file). These files must have the .lnk extension because the file extension is how Windows decides which handler to invoke when you double-click/execute the file. If you create a shortcut to a jpg file the real name can be link.jpg.lnk but the user will see the name as link.jpg in Explorer because .lnk is a special extension that is hidden.
A symbolic link (symlink). These are links on the filesystem level and can have any extension. mklink "c:\mylink.jpg" "c:\file.jpg"
A hardlink. This is another name for the same file (alias), it does not create a shortcut. mklink /H "c:\anothername.jpg" "c:\file.jpg"

Change location to shortcut location

I am launching a script with a powershell shortcut (C:\Windows....\powershell.exe -file 'D:\powershell\script.ps1').
Is there a way to make script change its current location to location of said shortcut?
Example: Script itself is in D:\powershell\ and the shortcut is in C:\Work\Project1. I need the script to cd to "C:\Work\Project1\".
Thanks
You can make a shortcut that starts in whatever directory it's located in. All you have to do is modify the "Starts In" property of the shortcut, and blank it out. That's right. Set it to nothing.
Then, when you want to invoke the shortcut, navigate to the folder (directory) where it's located before invoking it. I use this technique for a shortcut that launches powershell but doesn't launch a script. I haven't tested it with a shortcut that launches a script.
Walter Mitty's helpful answer provides an effective solution, provided that the shortcut file is opened from either the Desktop or File Explorer.
This answer provides background information.
You must configure the desired working directory as part of the shortcut file, because the script you invoke knows nothing about the shortcut that invoked it.
Therefore, to configure a specific working directory (e.g., C:\Work\Project1), specify it in the shortcut file's Properties dialog in the Start in: field.
In case you want to update a shortcut file's (*.lnk) working directory programmatically, use the technique from this answer with the .WorkingDirectory property.
Note: In both cases, only an absolute path can be configured as the working directory: File Explorer only allows you to enter an absolute path anyway, and while the programmatic method allows you to assign a relative path, it is instantly resolved to an absolute one, relative to the shortcut's location.
To make the shortcut's own directory the working directory, you can blank out the Start in: field / .WorkingDirectory property, but note the limitations:
Only works when such a shortcut is either opened from either the Desktop or from File Explorer.
By contrast, opening it from the taskbar or (pre-Windows 10 only) the Start Menu, the working directory is $env:windir\System32 (typically, C:\Windows\System32).
If the shortcut targets an application (rather than a document), as in this case, that application invoked by the shortcut - including cmd.exe and powershell.exe - starts in the configured / implied working directory.
Caveat re cmd.exe: If the working directory is specified as as a UNC path, cmd.exe won't be able to change to that directory; as a workaround, use a path with a mapped drive instead (but, obviously, that drive must be mapped at the time the shortcut is opened).
You can use the TargetPath property of the shortcut (maybe do a get-childitem $psscriptroot where name is like scriptname and extension is like .lnk) with the set-location cmdlet
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-location?view=powershell-6

How to create a universal windows shortcut?

I have created a shortcut for my program (under Windows 7), whose target is in a sub-directory. But when I zip everything and send it to my colleague, the shortcut wont work because it the target directory cannot be found, it is like:
Target: C:\Users\my_user_name\Desktop\my_program\sub_directory\my_program.exe
and the shortcut is in C:\Users\my_user_name\Desktop\my_program\
When I send it to another PC, my_user_name directory cannot be found and shortcut doesnt work. How to solve this?
Create a batch file that launches the program. You can then use a relative path.
e.g.
C:\Users\my_user_name\Desktop\my_program\launch.cmd
cd sub_directory
start my_program.exe
possible solutions:
place the program with the exe on disk like c:\program\programname\ Location must be the same on all computers.
place the program with the exe on the network where you can both access, shortcut is the same
edit the shortcut in notepad and change the user

Windows CMD: How to create symbolic link to executable file?

My goal is to add a few executables to my PATH (for example, chrome), so that I can call
> chrome
from the command prompt and it will launch Chrome.
I know I could add Chrome's containing directory to my path (set PATH=%PATH%<chrome_path_here>;), but since I have a few executables I want to add, I'd rather make a new bin directory that contains symbolic links to the actual executables and just add that single directory to my PATH.
The Chrome executable is located at
C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
So I tried
> mklink chrome.exe "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
That successfully creates a symbolic link for the files (says so in output, and upon examining with > dir). I know my PATH is set up correctly, b/c when I run > where chrome it finds my new symbolic link.
However, when I try to execute chrome with my new link, nothing happens. A new empty window should appear, but nothing happens. No error message in the command prompt or anything.
What am I doing wrong? Am I misunderstanding symlinks in Windows? This is the approach I use in Linux all the time, but I'm new to Windows Cmd.
Thanks!
Most programs will not run from places other than they install location - which is exactly what happens when you try to run it from symlink.
It would be much easier to create CMD/BAT files in that folder with matching names which will launch programs from locations you want:
REM chrome.cmd
start /b cmd /c "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" %*
With Windows 7 I confirm that symlinks do not work, are simply ignored as reported in the original question.
As Harry states in his comment, shortcuts do work, and to me are simpler and easier than writing a separate script for each new command I want to enable under CMD.
He states that you need to add .lnk to your PATHEXT variable in order to do this. I affirm that this does work, and with .lnk added to PATHEXT I can simply enter the name portion of the shortcut to run the command. For example if my shortcut is named "sublime.lnk" and PATHEXT includes .lnk, I can execute the link with the simple command "sublime". Nice!
As an alternative I found that PATHEXT need not be modified if I simply type in the full name of the shortcut, including the .lnk, at my CMD prompt. E.g., I created a shortcut named "sublime.lnk" under %HOMEPATH%/bin, pointing to "C:\Program Files\Sublime Text 2\sublime_text.exe".
Now by placing %HOMEPATH%\bin in my %PATH% I can run sublime via the command "sublime.lnk".
Either of the above are the best way I know of giving access to various commands from around Windows' filesystem from a CMD prompt. I'm not a Windows expert though, and welcome a better or more standardized solution to this problem.
P.S.: I just found out the hard way that you need to ensure the "Start in:" property of any shortcut you use in this fashion is blanked out, or your program will not start in the directory you invoke the shortcut from.
P.P.S.: On a related note, I discovered how to run Windows Explorer (or its replacement) on the directory your CMD session is logged in to: start ..

Locate a desktop shortcut with the command prompt or Powershell

I'm trying to use the command prompt or Powershell to locate some of the shortcuts that an installed program has created on my desktop. For Example, Adobe Reader has created a shortcut on my desktop. I try to use dir "c:\Users\MyUserName\Desktop", but I do not see this shortcut.
How can I find this shortcut through a command prompt or with powershell?
Shortcuts are files with extension ".lnk" and would be displayed by dir ( cmd and powershell)
I think some shortcuts (.lnk files) are created at c:\users\public\Desktop so that they can be shared by all users. ( shortcuts placed in this location are available on the desktop of all the users)
More about the "Public user" here: http://windows.microsoft.com/en-US/windows-vista/Sharing-files-with-the-Public-folder
So you may want to look in that location as well.

Resources