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

Introduction

This script demonstrates how to create a shortcut to run an application as a different user using PowerShell.

For VBScript version, please visit https://gallery.technet.microsoft.com/How-to-create-a-shortcut-6773b2cf

Scenarios

Using the script can create a shortcut that you can run from any user account to run an application as a different user on the desktop quickly. It saves you 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

Step1: Run the script in the Windows PowerShell Console, type one command in this form: Import-Module <Script Path> at the prompt.

 For example, type Import-Module C:\Scripts\CreateShortcutRunAsDiffUser.psm1

Step 2: You can type the command Get-Help CreateShortcutRunAsDiffUser –Full to display the entire help file for these functions including the syntax, parameters, or examples.

PowerShell
Edit|Remove
 #Create a Desktop Shortcut with Windows PowerShell 
    If(!$ShortcutFileName) 
    { 
        $ShortcutFileName = (Get-Item $TargetFile).Basename 
    } 
    $ShortcutFile = "$env:Public\Desktop\$ShortcutFileName.lnk" 
    $WScriptShell = New-Object -ComObject WScript.Shell 
    $Shortcut = $WScriptShell.CreateShortcut($ShortcutFile) 
    #Run as different user, saves the password   
    $Shortcut.TargetPath = "runas.exe" 
    $Shortcut.Arguments = "/savecred /user:$RunAsUser ""$TargetFile""" 
    $Shortcut.IconLocation = $TargetFile 
    $Shortcut.Save()  

Examples

PowerShell
Edit|Remove
CreateShortcutRunAsDiffUser -TargetFile "C:\Program Files\Internet Explorer\iexplore.exe" -RunAsUser "fareast\v-frta" -ShortcutFileName "Internet Explorer"

An Internet Explorer shortcut has been created to run as a “fareast\v-frta” user.


Prerequisites

Windows PowerShell 2.0 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.