Tinus EngOps Wiki

Logo

OpsaC - Operating as PowerShell code

Links

Home

IT-Wiki

PowerShell Blog

PowerShell Index

PowerShell Search

Additional Websites

View my GitHub Profile

View my GitHub Gists

View my Codeberg Page

View my Photo Website

Run PowerShell as

published: January 28, 2019 author: Tinu tags: PowerShell categories: PowerShell-Basic


Table of Contents

Run PowerShell as Administrator

Enter in a PowerShell as User:

Start-Process "$psHome\powershell.exe" -Verb Runas -Wait

for PowerShell Core enter:

Start-Process "$psHome\pwsh.exe" -Verb Runas -Wait

Run a PowerShell-Script as Administrator

[CmdletBinding()]
param(
    [ValidateSet("VMXNet3","E1000")]
    [Parameter(Mandatory = $true)]
    [String]$Interface
)

$ScriptFullName  = $MyInvocation.MyCommand.Source
$CurrentIdentity = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$IsAdministrator = $CurrentIdentity.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

if(-not($IsAdministrator)){
    Start-Process "powershell.exe" -ArgumentList "-File $($ScriptFullName) -Interface $($Interface)" -Verb Runas -Wait
}else{
    Write-Host "I'm running as Administrator $($Interface)!" -ForegroundColor Red
}
Read-Host -Prompt "Press any key to continue"

Run PowerShell as System

Enter in the Administrators PowerShell:

psexec.exe -i -s powershell.exe

PsExec is a tool written by Mark Russinovich (included in the Sysinternals Suite) and can downloaded here.

See also

Windows Sysinternals on Microsoft Docs


← Previous Post ◇ [ Top ] ◇ Copyright © 2024 by tinuwalther ◇ [ Blog ] ◇ Next Post →