How can I parse a .LNK shortcut from the Command Prompt in Windows?

When I start CMD, I see C:\Users\dave> but I want to change directory to C:\wamp\www without having to press cd ../../. So I created a shortcut of the 'www' folder in the 'dave' folder.

This is a typical X-Y problem. If you want CMD to always open at a specific directory instead of the default, all you need to do is simply change the shortcut's properties as follows:

enter image description here

In Windows 7 the Command Prompt shortcut is typically located in Start Menu > All Programs > Accessories, so just right-click the shortcut, select Properties and edit the Start in field to your liking.


You can also create a batch file named for example d.bat that contains a single line cd /d c:\wamp\www. Place the batch file somewhere in your path and now all you need to do is open CMD and type d to change to the specific directory. There are many more similar solutions as well.


If you are dead set on parsing a shortcut (.LNK) file from the command prompt, save the following as ParseLnk.bat and execute it from the Command Prompt as ParseLnk <LNK File>:

@echo off
echo set WshShell = WScript.CreateObject("WScript.Shell")>Tmp.vbs
echo set Lnk = WshShell.Createshortcut(WScript.Arguments(0))>>Tmp.vbs
echo WScript.Echo Lnk.TargetPath>>Tmp.vbs
for /f "delims=" %%d in ('cscript //nologo Tmp.vbs "%~1"') do del Tmp.vbs & cd /d "%%d"

In case anyone got to this question like I did, wanting to decipher a .lnk file using only the command prompt, try:

type mylink.lnk|find "\"

I think the problem is a .lnk file is a document that is interpreted by a program - Explorer.exe - just like a docx file would be handled by Word, or whatever. You'd have to write some sort of script to parse the lnk file and excecute a cd command.

It is possible there is the ability to do what you want in PowerShell (as opposed to Command Prompt) through a cmdlet, either built-in, third-party, or one you could write yourself, but I do not know.