[VBS and Batch] Create a Shortcut from CMD or Script

Discussion in 'Scripting' started by hearywarlot, Aug 12, 2015.

  1. hearywarlot

    hearywarlot MDL Member

    Jul 31, 2015
    112
    154
    10
    #1 hearywarlot, Aug 12, 2015
    Last edited by a moderator: Apr 20, 2017
    This is a VBS which can be used to create shortcuts to folders or files.

    CMD/Batch usually does not support creating shortcuts other than Symbolic links or Junctions.
    So you usually need external tools, but luckely VBS can create shortcuts and accept switches.

    Usage:
    The script will accept 6 switches.

    The switches:
    Required:​

    • /target:"<Target>"
    • /shortcut:"<Shortcut Name>"
    Optional:​

    • /parameters:"<Arguments for target>"
    • /startin:"<Start in folder>"
    • /description:"<Description>"
    • /icon:"<Custom Icon>"
    If you pass parameters and need to use quotes, add a flower "\'/" to your argument. This will be replaced by a quote on parsing.

    You can execute the VBS from a script or from CMD.
    Code:
    CreateShortcut.vbs /target:"%WINDIR%\system32\cmd.exe" /parameters:"/C COLOR 1f & echo \'/By yours truly hearlywarlot :)\'/ & pause" /startin:"%~dp0" /shortcut:"Cmd to This Folder" /description:"Open CMD to this scripts directory" /icon:"%WINDIR%\explorer.exe"
    Edit this example command to suit your needs.

    Notes:
    Since VBScript will not handle nicely quotes passed as string from CMD without making code ugly, I added \'/ as replacement which will turn to quotes on parsing.
    If you need these specific characters in arguments, change these characters in the VBScript to something else that will not bring you trouble.

    Code:
    Filename: CreateShortcut.vbs
    Code:
    set WshShell = WScript.CreateObject("WScript.Shell" )
    Set colNamedArguments = WScript.Arguments.Named
    iNumberOfArguments = WScript.Arguments.Count
    
    if iNumberOfArguments < 1 then
        Wscript.Echo "Switches:",vbCrLf,"/target:<Target>",vbCrLf,"/shortcut:<Shortcut Name>",vbCrLf,"/parameters:{Arguments for target}",vbCrLf,"/startin:{Start in folder}",vbCrLf,"/description:{Description}",vbCrLf,"/icon:{Custom Icon}"
        WScript.Quit 1
    ElseIf Not colNamedArguments.Exists("target") Then
        Wscript.Echo "Switch /target:<Target> is missing."
        WScript.Quit 1
    ElseIf Not colNamedArguments.Exists("shortcut") Then
        Wscript.Echo "Switch /shortcut:<Shortcut Name> is missing."
        WScript.Quit 1
    End If
    
    set oShellLink = WshShell.CreateShortcut(Wscript.Arguments.Named("shortcut") & ".lnk")
    oShellLink.TargetPath = Wscript.Arguments.Named("target")
    
    If colNamedArguments.Exists("parameters") Then
        arg = Wscript.Arguments.Named("parameters")
        oShellLink.Arguments = Replace(arg, "\'/", chr(34))
    End If
    If colNamedArguments.Exists("startin") Then
        oShellLink.WorkingDirectory = Wscript.Arguments.Named("startin")
    End If
    If colNamedArguments.Exists("description") Then
        oShellLink.Description = Wscript.Arguments.Named("description")
    End If
    If colNamedArguments.Exists("icon") Then
        oShellLink.IconLocation = Wscript.Arguments.Named("icon")
    End If
    
    oShellLink.Save
    WScript.Quit
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. sebus

    sebus MDL Guru

    Jul 23, 2008
    6,384
    2,037
    210
  3. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    895
    482
    30
    Or just nircmd.exe shortcut

    nircmd gets deployed on any machine i use or have to do with for a longer period.
     
  4. hearywarlot

    hearywarlot MDL Member

    Jul 31, 2015
    112
    154
    10
    #4 hearywarlot, Aug 13, 2015
    Last edited: Aug 13, 2015
    (OP)
    I also like using NirCmd or practically any program from Nirsoft and I recommend it to anyone who need extra functions which cmd.exe does not support.

    But unfortunantly it is a external program which you usually would not find on Windows unless you add it yourself.
    So having a VBS which you can call or create from a script to make shortcuts is nice to have, since you know it will just work.

    My favorites are: BluescreenView, Produkey, Mail PassView, ShellExView, WhatIsHang, AlternateStreamView and HashMyFiles.
    You can also download all the programs from Nirsoft in a package with launcher called 'NirLauncher'.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. Kamrul08

    Kamrul08 MDL Member

    Dec 28, 2013
    104
    13
    10
    Thanks hearywarlot.
     
  6. LiteOS

    LiteOS Windowizer

    Mar 7, 2014
    2,343
    1,047
    90
    thx for this
    Would be nice to add Arguments like

    explorer.exe c:\
     
  7. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,694
    267
    60
    This makes very interesting reading.
    I am after making shortcuts with .vbs files.
     
  8. hearywarlot

    hearywarlot MDL Member

    Jul 31, 2015
    112
    154
    10
    #8 hearywarlot, Feb 4, 2016
    Last edited: Feb 4, 2016
    (OP)
    Your wish has been fullfilled ;).
    You can now add arguments with the '/parameters' switch.
    If you need to use quotes, add a flower "\'/". This will be replaced by a quote on parsing.

    Strange as it may seem, VBScript will not handle nicely quotes passed as string from CMD without making code ugly.

    For reference and example look at my first post.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...