Always Run PowerShell ISE As Admin

21,725

Solution 1

Here are 2 different options you can use to start ISE as admin:

  1. You can create/edit a shortcut to the powershell_ise.exe executable file and edit the properties for that shortcut to Run as Administrator.

enter image description here

  1. You can start PowerShell ISE as admin by Start-Process powershell_ise -verb RunAs.

Solution 2

I came across this many months after the post, but I think my comment is helpful.

@Deadly-Bagel No, the script is just a file so it doesn't "run".

While a script is just a file, it can certainly contain functions. One of those functions can be setup to run as the first process in that script file. That function can be used to determine whether or not the current session is running as an administrator. If it is not, it can self-elevate (assuming the logged-in user has admin rights).

    function Use-RunAs
    {    
    # Check if script is running as Adminstrator and if not use RunAs 
    # Use Check Switch to check if admin 

    param([Switch]$Check) 

    $IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()` 
    ).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator") 

    if ($Check) { return $IsAdmin }     

    if ($MyInvocation.ScriptName -ne "") 
    {  
        if (-not $IsAdmin)  
        {  
            try 
            {  
                $arg = "-file `"$($MyInvocation.ScriptName)`"" 
                Start-Process "$psHome\powershell.exe" -Verb Runas -ArgumentList $arg -ErrorAction 'stop'  
            } 
            catch 
            { 
                Write-Warning "Error - Failed to restart script with runas"  
                break               
            } 
            exit # Quit this session of powershell 
        }  
    }  
    else  
        {  
            Write-Warning "Error - Script must be saved as a .ps1 file first"  
            break  
        }
    } 

    Use-RunAs

I hope this helps your situation. If not, I'd be happy to revisit.

Share:
21,725

Related videos on Youtube

Deadly-Bagel
Author by

Deadly-Bagel

Working in general IT support, managing hundreds of servers, but branching into scripting and coding as a hobby and now bringing it in to the work environment as well. Avid gamer at home but not particularly social.

Updated on September 18, 2022

Comments

  • Deadly-Bagel
    Deadly-Bagel over 1 year

    I have Windows 10 and run some PowerShell commands through the Outlook COM object. This requires PowerShell to be running the same level of access (running as admin or user) as Outlook.

    As I need PowerShell to be running as administrator for other pieces I set Outlook to always run as admin too, no problem. The issue is when I open the ISE I now can't access the Outlook objects.

    There doesn't appear to be any way to set the ISE to always run as admin. The Compatibility tab is missing (though not on PowerShell itself?) and the registry keys in ...Windows NT\AppCompatibility did nothing. I'd disable UAC completely but of course it's not possible in Windows 10 without screwing over a bunch of other stuff.

    Any ideas? And no I don't particularly feel like right clicking the icon each time as I often open scripts directly.

    UPDATE: Outlook complains it can't index as admin and the ISE doesn't recognise my network drives as admin so the lesser of two evils is just run both as a user. Incidentally, the Compatibility tab has disappeared from the PowerShell exe now too, thanks Microsoft...

  • bentek
    bentek over 8 years
    No, it's 2 different options.
  • bentek
    bentek over 8 years
    Updated answer to clarify that.
  • Ramhound
    Ramhound over 8 years
    I Appreciate the effort to improve the answer.
  • Deadly-Bagel
    Deadly-Bagel over 8 years
    This does not resolve the issue as if I open the ISE by double clicking a script the ISE opens as a restricted user. I would need to open the ISE from the shortcut every time. Seems that's the direction Microsoft are going though: You can do it this way and it does work, but not as well as it did in Windows 7 / Server 2008
  • bentek
    bentek over 8 years
    Can you set the script properties to run as admin then?
  • Deadly-Bagel
    Deadly-Bagel over 8 years
    No, the script is just a file so it doesn't "run". Additionally I have discovered that if the ISE is run as admin you cannot open scripts on network drives. It just doesn't recognise the path. Windows 10 has its advantages (esp. running four screens) but I keep running into junk like this and it's getting annoying.
  • YetAnotherRandomUser
    YetAnotherRandomUser almost 7 years
    Option 1 is (or is now) available on Windows server 2012 R2 using Classic Shell. The Windows 10 start menu doesn't seem to give you shortcut properties, but you can access them when the program is running via its icon.
  • Deadly-Bagel
    Deadly-Bagel over 6 years
    That's if you're executing the script, my problem was that the ISE opens as a user when editing the script and was therefore restricted. Might work if you limited yourself to testing with F8... Unfortunately I found that when the ISE runs as admin it loses access to all my network drives for some reason, and Outlook was complaining at being admin so I dropped them both to user and the stuff that needs testing as admin (most of it) I just copy to an admin console. Having said that, I think I have a different use for this script so thanks for taking the time =)