# Creating Shortcuts with Batch File



## Flava0ne (Aug 31, 2014)

I've created a Batch file which calls on the following VB Script to be able to create a shortcut to my desktop:

*VB Script (named createLink.vbs):*

```
set objWSHShell = CreateObject("WScript.Shell")
set objFso = CreateObject("Scripting.FileSystemObject")

' command line arguments
' TODO: error checking
sShortcut = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
sTargetPath = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
sWorkingDirectory = objFso.GetAbsolutePathName(sShortcut)

set objSC = objWSHShell.CreateShortcut(sShortcut)

objSC.TargetPath = sTargetPath
objSC.WorkingDirectory = sWorkingDirectory

objSC.Save
```

*Batch File Example (for Windows Notepad):*

```
@echo off
cscript createLink.vbs "C:\Users\Administrator\Desktop\Notepad.lnk" "C:\Windows\System32\notepad.exe"
```

But after creating the shortcut using this method, I'm not able to assign a custom icon for the shortcut. I get the following error message when trying to do so:







Any help and ideas on what I need to put into the VB Script and/or Batch File to be able to do this would be greatly appreciated! 

Also, I know that I can simply do this by going to *Start Menu\All Programs\Accessories\* right-clicking on *Notepad* and selecting *Send to\Desktop (create shortcut)*. Easy enough for creating a single shortcut. But I need to create several multiple shortcuts and using this Batch/VBS method would really save me a lot of time in doing so.


----------



## FordGT90Concept (Aug 31, 2014)

Working Directory needs to match the directory of target which would be "C:\Windows\System32"

You set the icon via: objShortcut.SetIconLocation "C:\Windows\System32\SHELL32.dll", 13

Path is obvious, the number is the index of the icon inside the DLL, EXE, or ICO.  You should be able to use Greenfish Icon Editor Pro to see the indices in an ICO file.  You can use Resource Hacker to view the indicies in a DLL or EXE.


----------



## Flava0ne (Aug 31, 2014)

FordGT90Concept said:


> Working Directory needs to match the directory of target which would be "C:\Windows\System32"
> 
> You set the icon via: objShortcut.SetIconLocation "C:\Windows\System32\SHELL32.dll", 13
> 
> Path is obvious, the number is the index of the icon inside the DLL, EXE, or ICO.  You should be able to use Greenfish Icon Editor Pro to see the indices in an ICO file.  You can use Resource Hacker to view the indicies in a DLL or EXE.



How would the VB script look like with that line you mentioned added into it? I didn't create this VB Script and I'm not very familiar with using it. I'm more familiar with using Batch Command Files. I've created my very own custom icons for practically every program on my system. I usually use Restorator 2007 to edit the icons themselves in the EXE files for the programs but there are some program's that apparently won't work if doing this. So for the one's that won't work with that method, I place the icon(s) in the Pictures folder and then make it hidden after assigning it to the shortcut.


----------



## FordGT90Concept (Aug 31, 2014)

```
set objWSHShell = CreateObject("WScript.Shell")
set objFso = CreateObject("Scripting.FileSystemObject")

sShortcut = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
sTargetPath = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
sWorkingDirectory = objFso.GetAbsolutePathName(sTargetPath)

set objSC = objWSHShell.CreateShortcut(sShortcut)
objSC.TargetPath = sTargetPath
objSC.WorkingDirectory = sWorkingDirectory
objShortcut.SetIconLocation(sTargetPath, 9)
objSC.Save
```
Or if you want to control the icon index from batch:

```
set objWSHShell = CreateObject("WScript.Shell")
set objFso = CreateObject("Scripting.FileSystemObject")

sShortcut = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
sTargetPath = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
sWorkingDirectory = objFso.GetAbsolutePathName(sTargetPath)

set objSC = objWSHShell.CreateShortcut(sShortcut)
objSC.TargetPath = sTargetPath
objSC.WorkingDirectory = sWorkingDirectory
objShortcut.SetIconLocation(sTargetPath, WScript.Arguments.Item(2))
objSC.Save
```
Batch:

```
@echo off
cscript createLink.vbs "C:\Users\Administrator\Desktop\Notepad.lnk" "C:\Windows\System32\notepad.exe" 9
```


----------



## Flava0ne (Aug 31, 2014)

Tried both of the VB Scripts that you provided and adding the 9 switch to the batch file but neither was able to create a shortcut to my desktop.


----------



## FordGT90Concept (Aug 31, 2014)

What was the error?  Maybe it is better to move to a program to do this instead of VBS.


----------



## Flava0ne (Sep 1, 2014)

No error, when I run the BAT file the CMD prompt shows and looks like it did stuff. But no shortcut on my Desktop.


----------



## FordGT90Concept (Sep 1, 2014)

Threw a program together quick for you.  It is attached with source in the other.  Use it in place of your VBScript.

It supports:
CreateShortcut [save shortcut] [path to run]
CreateShortcut [save shortcut] [path to run] [icon index]
CreateShortcut [save shortcut] [path to run] [icon file]
CreateShortcut [save shortcut] [path to run] [icon file] [icon index]

If "icon index" is missing, it assumes 0.  If "icon file" is missing it assumes "path to run."  It doesn't presently support arguments to be added to the shortcut nor modifying the working directory (always assumes same directory as "path to run").

Requires .NET Framework 4.0 to run.  Will run natively on 32-bit and 64-bit versions of Windows.


----------



## Flava0ne (Sep 1, 2014)

So basically my Batch File will now look like this:


```
@echo off
CreateShortcut.exe "C:\Users\Administrator\Desktop\Notepad.lnk" "C:\Windows\System32\notepad.exe"
```

Thanks man!  This does exactly what I want done and I'm able to customize the icon for the shortcut without problems. But I'm not quite sure how to use the [icon file] portion you mentioned that it supports. I tried the following Batch File:


```
@echo off
CreateShortcut.exe "C:\Users\Administrator\Desktop\Notepad.lnk" "C:\Windows\System32\notepad.exe" "C:\Users\Administrator\Pictures\Notepad.ico"
```

It created the shortcut but didn't change the icon.


----------



## FordGT90Concept (Sep 1, 2014)

Are you sure Notepad.ico exists in that location?  If it does, maybe you should zip it up and attach it so I can try myself and see if I can catch an error.


----------



## Flava0ne (Sep 1, 2014)

Yes, the icon is in that location. Anyways, I zipped it up and attached it. It's just the icon itself.


----------



## FordGT90Concept (Sep 1, 2014)

Hmm, I just tried it and it worked fine.  Maybe try running the batch as administrator?  Also, right click on the desktop after the shortcut is created and click Refresh.

If the file in the third parameter doesn't exist, it assumes the icon is in the application.


----------



## Flava0ne (Sep 1, 2014)

FordGT90Concept said:


> Hmm, I just tried it and it worked fine.  Maybe try running the batch as administrator?  Also, right click on the desktop after the shortcut is created and click Refresh.
> 
> If the file in the third parameter doesn't exist, it assumes the icon is in the application.



Yeah, refreshing did it, well not exactly, more like updated it lol. Thanks man you really saved me a lot of time with this, much appreciated!


----------



## Flava0ne (Sep 11, 2014)

How would I assign specific icon that's inside an EXE within the program itself?


----------



## FordGT90Concept (Sep 11, 2014)

That's what the icon index is for.  This one:
CreateShortcut [save shortcut] [path to run] [icon index]

You can use Resource Hacker to view the indices.  Or you can just guess and check.


----------



## Flava0ne (Sep 11, 2014)

So for the icon index, should I just set the path to the EXE followed by "\[x]" (x being the index of the specific icon)?


----------



## FordGT90Concept (Sep 11, 2014)

No, example: CreateShortcut "C:\shortcut.lnk" "C:\program.exe" 6

That would set icon index 6 in program.exe to the icon in shortcut.lnk.  No slash and no quotes, just a number.


----------



## Flava0ne (Sep 11, 2014)

Okay gotcha! Thanks again man!


----------



## Wastedslayer (Feb 19, 2015)

This little program just saved me a whole heap of time with pushing out some new software in a school. Until our AD is cleaned up/organized pushing something like this out via GPO would have been a nightmare... while I still have to have my guys touch every machine, using this program allows them to just run a simple script and wait for the okay. Without this I would still be trying to make a VB script work.

Thanks so much!


----------



## FordGT90Concept (Feb 19, 2015)

FYI, here is batch code to make Vista and newer demand admin access:

```
@echo OFF
:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------
```
If you need to use it, place all of the administrative batch lines after this and they'll run with admin rights.  Basically, it elevates the batch to administrator access and then passes those administrator credentials to all subsequent applications.


----------

