SU.PS1 Script

The following script allows you to run another PowerShell as a different user.  I use this sort of functionality when I’m testing software on another account (typically a standard user account).  I know, bad dev, I should be running as standard user instead of admin.  Oh well, I’ll make that switch when I move to Vista.
 
Save this script to a file called something like SU.PS1.
 
#######################################################################
# Author: Keith Hill
#         with help from Tony at http://mshforfun.blogspot.com/ 
#         and William Stacey
#
# Desc: Launch a new powershell.exe with someone else’s identity
#
# To enable console prompting execute the following:
#
# $key = "HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell"
# Set-ItemProperty $key ConsolePrompting True
#
#######################################################################
$cred = get-credential $null
$domain = "."
$user = $cred.UserName
$username = $cred.UserName
if ($user.IndexOf(‘\’) -ge 0)
{
    $tokens = $cred.UserName.Split(‘\’)
    $domain = $tokens[0]
    $user = $tokens[1]
}
 
$StartInfo = new-object System.Diagnostics.ProcessStartInfo
$StartInfo.FileName = "${PSHOME}\powershell.exe"
$StartInfo.Arguments="-noexit -command `$Host.UI.RawUI.WindowTitle=\`"Windows PowerShell ($($username)) \`""
$StartInfo.UserName = $user
$StartInfo.Password = $cred.Password
$StartInfo.Domain = $domain
$StartInfo.LoadUserProfile = $true
$StartInfo.UseShellExecute = $false
$StartInfo.WorkingDirectory = (get-location).Path
$proc = [System.Diagnostics.Process]::Start($StartInfo)
"Started new PowerShell as user: $username in process id: $(${proc}.Id)"
""
Advertisements
This entry was posted in PowerShell. Bookmark the permalink.

2 Responses to SU.PS1 Script

  1. Tony says:

    Actually, "Su" means switch user. So you can switch to Administrators or switch to Users

  2. Keith says:

    Yeah according to Wikipedia it is subsitute user.  Same thing I guess as switch user so SU.PS1 is a fine name then plus it is a shorter name.  🙂
     
    http://en.wikipedia.org/wiki/Su_%28Unix%29

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s