Create Shortcuts for “RunAs”-command using PowerShell

Markus Kolbeck
Markus' Blog
Published in
1 min readSep 26, 2014

If you ever wanted to create shortcut files (*.lnk) using powershell, you might have had troubles finding the correct syntax for the runas command as the target.

Here’s a script sample that creates a shortcut file on the desktop for a provided username (sAMAccountName).

It first queries AD (Active Directory) for the user’s first and lastname to make the shortcut look prettier.
Make sure you have Remote Server Administration Tools (RSAT) installed to use dsquery & dsget, otherwise just replace those lines.

Then it calculates the TargetPath & Arguments for running Internet Explorer as a different user.

$URL = "http://domain.com/sites/Site/page.aspx"
$username = "Bob"
$ln = $(dsquery user -samid $username | dsget user -ln)[1]
$fn = $(dsquery user -samid $username | dsget user -fn)[1]
$userDisplayName = $fn + $ln
$wshShellObject = New-Object -com WScript.Shell
$userProfileFolder = (get-childitem env:USERPROFILE).Value
$programFilesFolder = (get-childitem env:"ProgramFiles(x86)").Value
$WindowsFolder = (get-childitem env:windir).Value
$UserDomain = (get-childitem env:USERDOMAIN).Value
$wshShellLink = $wshShellObject.CreateShortcut($userProfileFolder+"Desktop"+$username+" -"+$userDisplayName+".lnk")$wshShellLink.TargetPath = $WindowsFolder + "System32runas.exe"
$wshShellLink.Arguments = '/user:'+$UserDomain+''+$username+' '+$programFilesFolder+'"Internet Exploreriexplore.exe ' + $URL
$wshShellLink.WindowStyle = 1
$wshShellLink.IconLocation = $programFilesFolder +"Internet Exploreriexplore.exe"$wshShellLink.WorkingDirectory = "C:Windowssystem32"$wshShellLink.Save()#PoSh

--

--