How to create a shortcut to run an application as a different user by VBScript

Introduction

This script demonstrates how to create a shortcut to run an application as a different user by VBScript.

For PowerShell version, please visit https://gallery.technet.microsoft.com/How-to-create-a-shortcut-54f6e10f

Scenarios

Using the script can create a shortcut to run from any user account so that an application can be run as a different user on the desktop quickly. It saves your time from shift > right click > run as different user > enter user name > enter password every time you want to run an application as a different user.

Script

• Double click the "DetermineIISVersion.vbs" to run this VBScript sample

• Confirm the privilege elevation request of the pop-up window

• After the script runs, it will pop up an input box as follows.


 

• As you can see, the Internet Explorer shortcut has been created to run as a “fareast\v-frta” user.


 

• Here are some code snippets for your reference.

 

VB Script
Edit|Remove
targetFile = InputBox("Please input the application file path.","","C:\Program Files\Internet Explorer\iexplore.exe") 
runAsUser = InputBox("Please input the user account."Set objFSO = CreateObject("Scripting.FileSystemObject")  
If (objFSO.FileExists(targetFile)) Then  
    Set objFile = objFSO.GetFile(targetFile)  
    shortcutFileName = objFile.Name   
    Set objFile = Nothing 
    Set WshShell = WScript.CreateObject("WScript.Shell") 
    Set shortcut = WshShell.CreateShortcut(WshShell.SpecialFolders("Desktop") & "\" & shortcutFileName & ".lnk") 
    shortcut.TargetPath = "runas.exe" 
    shortcut.Arguments = "/savecred /user:" & runAsUser & " """& targetFile &"""" 
    shortcut.IconLocation = targetFile 
    shortcut.Save 
    Set WshShell = Nothing   
Else  
    WScript.Echo "Not find the application." 
End If  
Set objFSO = Nothing

Prerequisites

Windows 7 or higher version

Microsoft All-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.