Posted On August 29, 2019

PowerShell: How to Call a Batch File to Run-As Administrator

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: How to Call a Batch File to Run-As Administrator

Step 1: Create a Batch File and place it inside C:\scripts

@echo off
powercfg.exe -x -monitor-timeout-ac 0
powercfg.exe -x -monitor-timeout-dc 0
powercfg.exe -x -disk-timeout-ac 0
powercfg.exe -x -disk-timeout-dc 0
powercfg.exe -x -standby-timeout-ac 0
powercfg.exe -x -standby-timeout-dc 0
powercfg.exe -x -hibernate-timeout-ac 0
powercfg.exe -x -hibernate-timeout-dc 0

Step 2: Create a PowerShell file in the same directory and place a shortcut on the Desktop

var startProcess = new System.Diagnostics.ProcessStartInfo();
        newProcessInfo.FileName = @"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
        startProcess.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; # run in the background
        startProcess.Verb = "runas"; # run as [administrator]
        startProcess.Arguments = @"-executionpolicy unrestricted -Command ""C:\scripts\powercfg.bat -noexit"""; # set powershell switches to call the batch files to run unrestricted. Remove the -noexit item to close the window automatically after each run time
        System.Diagnostics.Process.Start(startProcess); # Trigger the start command to instantiate and bowow

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Check Registry Path, Key, and Dword Value

$path='HKLM:\Software\Intel' $keyName='GMM' $dwordName='DedicatedSegmentSize' $dwordValue=512 Function CheckRegistryKey { param( [Parameter(Position = 0, Mandatory = $true)][String]$path, [Parameter(Position…

User Account Group Membership Copy

This is the quick snippet to be executed in the context of a Domain Administrator:…

NaN

- Meaning: Not a Number - When used as an assignment to an element index…