How can I run commands in PowerShell with Administrator permissions?

1

I would like to run commands in PowerShell with Administrator permissions? How can I do that?

I have tried to start PowerShell with the runas command, but PowerShell is closed immediately after I have typed a password.

I use Windows 7 and I am the only user on the computer.

Jonas

Posted 2010-12-09T14:10:04.767

Reputation: 21 007

Duplicate question - http://superuser.com/questions/108207/how-to-run-a-powershell-script-as-administrator

– vapcguy – 2015-07-16T17:56:11.517

You can use gsudo to elevate PowerShell commands: $hash = gsudo "(Get-FileHash ./protectedfile).Hash""

– Gerardo Grignoli – 2020-02-19T14:12:20.153

Answers

2

Usually when I see powershell immediately close, its a problem with the Execution Policy. clicking the Orb, typing Powershell then Right Clicking the link, and "run as Administrator" and have it open that way?

You can then, to see whats going on, navigate to the folder where your script is, and do a ./NameofScript.ps1

Brian

Posted 2010-12-09T14:10:04.767

Reputation: 2 934

What is the Orb that I should click? – Jonas – 2010-12-09T15:04:25.260

3@Jonas I think he means The UI Element Formerly Known As The Start Button. – Shinrai – 2010-12-09T16:00:54.717

@Shinrai, what the heck are we supposed to call it now? Is there a new official term? – Brian – 2010-12-09T22:34:45.527

I have no clue. I like yours, actually. ;) (Also, sorry, I forgot to +1 before I was so busy being clever, ahah) – Shinrai – 2010-12-09T23:07:21.377

...actually, the Help files in Win7 still call it the "Start button". Guess that's official. – Shinrai – 2010-12-09T23:08:44.623

2

If you are already in powershell, type: Start-Process powershell -verb runas

I write a sudo function to accomplish more powerful things, like executing something elevated and get the result in the same shell, for example:

sudo {rm fileThatNeedElevatedRightsToBeDeleted; ls}

Here is the code:

function sudo(){
param([String] $code)
$viejos = gps powershell
$here = add-quotes (get-location).path
$resultPath = [IO.Path]::GetTempPath() + "temp.result.xml";

$code = "set-location $here; function Run{$code};Run $args|Export-Clixml $resultPath" 

$encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($code))

start-process PowerShell.exe -verb Runas -argumentlist '-encodedCommand',$encoded

$nuevos = gps powershell
$array = New-Object Collections.Generic.List[int]
$array2 = New-Object Collections.Generic.List[int]
$viejos | %{$array.add($_.ID)}
$nuevos | %{$array2.add($_.ID)}
$idTowait = $array2 | ?{$array -notcontains $_}
while(1){
    $nuevos = gps powershell
    $array2.Clear()
    $nuevos | %{$array2.add($_.ID)}
    if($array2 -contains $idTowait)
        {sleep -Milliseconds 500}
    else
        {break;}
}

if(Test-Path $resultPath){
  if((gi $resultPath).length)
  {        
      Import-Clixml $resultPath 
      rm $resultPath     
  }
}
else
    {"No results"};
}

mjsr

Posted 2010-12-09T14:10:04.767

Reputation: 5 577

0

For administrative task I have most of the time an instance of PowerShellISE running. I copied the link to start ISE from All Programs | Accessories | Windows PowerShell | Windows PowerShell ISE to the taskbar. To start ISE with Admin privileges I press the keys shift + control while left clicking the taskbar icon. After replying to the UAC dialog it ready.

Note: With regular ISE I nearly never use the Open File Dialogs, but drag files from some Explorer window into ISE. With ISE running in admin mode, this drag & drop is not possible for some security reason.

bernd_k

Posted 2010-12-09T14:10:04.767

Reputation: 1 181

I was with you up until "shift + control". That's for selecting multiple objects. To run as an administrator, just do a right-click and select "Run as administrator" - keep it simple. – vapcguy – 2015-07-16T17:50:50.613

0

PowerShell ISE lives at %windir%\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe. You can right-click that and select "Run as administrator" and run the script from in there.

You can also find it under the Windows Logo > All Programs > Accessories > Windows PowerShell and do the same thing with those shortcuts.

vapcguy

Posted 2010-12-09T14:10:04.767

Reputation: 111