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

Check Whether an Entity Has Access to a Directory or Its Children

Write-Host "This script just be ran in the context of a File Server Administrators member"…

PowerShell: Microsoft SQL Database Migration

Update 9/25/20: There's another version of this script has has been rewritten form scratch. IMO,…

PowerShell: Export Event Logs by a Certain Date Range

# User defined variables $daysLimit=7 $startdate=(get-date).adddays(-$daysLimit) $computername=$env:computername $logName='Application' $filterTypes='Error','Warning' $logPath="c:\temp\eventlogs-from-$($startdate.tostring('yyyy-MM-dd'))-to-$((get-date).tostring('yyyy-MM-dd')).csv" # Get the event logs…