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: Optimize RAM on Remote Computers

# simultaneousExec_v0.01.ps1# Requirement: must run as Administrator$computerNames="SHERER1","SHERER2"function installEmptyStandbyList{ $emptyStandbyListAvailable=(Get-Command EmptyStandbyList.exe -ErrorAction SilentlyContinue); if(!($emptyStandbyListAvailable)){ # Set…

PowerShell: Quick EMCopy

# Short Version $arr=@(); $arr+=[PSCustomObject]@{From='D:\Someshare';To='\\NEWSERVER\Someshare'} $arr+=[PSCustomObject]@{From='D:\Test';To='\\NEWSERVER\Test'} # Normal copying # $arr|%{emcopy $_.From $_.To /s /de…

PowerShell: Function to Confirm Certain Contents

It's always a good idea to receive user confirmations prior on proceeding to making important…