• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

Creating Shortcuts with Batch File

Joined
Mar 22, 2008
Messages
580 (0.09/day)
System Name X-Class MKII
Processor Intel Core i7-6950X Extreme Edition 4.60GHz 25MB Cache 14nm LGA2011-3 Broadwell-E 10-Core Processor
Motherboard EVGA Intel X99 Classified Dual Quad-Channel DDR4 Socket LGA2011-3 E-ATX Motherboard on BIOS v2.09
Cooling BP Summit M CPU WB/BP EVGA K|NGP|N FCWB/Nemesis 480GTR/Nemesis 560GTR/MMRS 150mm/Swiftech MCP655
Memory Corsair Dominator Platinum 128GB (8×16GB) @ 2800MHz 14-16-16-36 DDR4 PC4-22400 Memory Kit
Video Card(s) EVGA nVidia GeForce GTX 1080 Ti K|NGP|N 2139MHz 11264MB @ 12992MHz GDDR5X Video Cards in 2-Way SLI
Storage Samsung 970 PRO M.2 1TB NVMe V-NAND SSD on AquaComputer kryoM.2 PCIe 3.0 ×4 M.2 Adapter
Display(s) LG 34GN850-B 34” UltraGear 21:9 WQHD Nano IPS 1ms 144Hz HDR Gaming Monitor
Case CaseLabs MAGNUM SMA8 Gunmetal Single Wide Full-Tower Chassis with Custom Smoke Window Panel
Audio Device(s) Creative Sound Core3D On-Board 5.1 Surround Sound Quad-Core Audio Processor
Power Supply Corsair Professional Series AX1500i 1500-Watt 80+ TITANIUM Fully-Modular Digital Power Supply Unit
Mouse Logitech G502 PROTEUS SPECTRUM Core 200-12000DPI Laser Gaming Mouse
Keyboard Logitech G19 Gaming Keyboard with Full Color GamePanel LCD
Software Microsoft Windows 10 Professional 64-bit
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):
Code:
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):
Code:
@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:

Error503.jpg


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! :D

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.
 
Last edited:
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.
 
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.
 
Code:
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:
Code:
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:
Code:
@echo off
cscript createLink.vbs "C:\Users\Administrator\Desktop\Notepad.lnk" "C:\Windows\System32\notepad.exe" 9
 
Last edited:
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.
 
What was the error? Maybe it is better to move to a program to do this instead of VBS.
 
No error, when I run the BAT file the CMD prompt shows and looks like it did stuff. But no shortcut on my Desktop.
 
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.
 

Attachments

Last edited:
So basically my Batch File will now look like this:

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

Thanks man! :D 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:

Code:
@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.
 
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.
 
Yes, the icon is in that location. Anyways, I zipped it up and attached it. It's just the icon itself.
 

Attachments

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.
 
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! :D
 
How would I assign specific icon that's inside an EXE within the program itself?
 
Last edited:
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.
 
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)?
 
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.
 
Okay gotcha! Thanks again man!
 
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!
 
FYI, here is batch code to make Vista and newer demand admin access:
Code:
@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.
 
Back
Top