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: Create Registry Keys within Windows Localhost

# createRegKey.ps1 $regKeys=@( @{ hive='REGISTRY::HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome'; name='ChromeCleanupEnabled'; value=0 } @{ hive='REGISTRY::HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome'; name='ChromeCleanupReportingEnabled'; value=0 } ) function…

PowerShell: Compressing Files and Folders

# Set variables $compressTarget="C:\Temp\Win2016_Std_Template.ova" $parentFolder=split-path $compressTarget -Parent $grandParentFolder=split-path $parentFolder -Parent $compressedFile=$grandParentFolder+"\compressed.zip" # PowerShell version 5…

Script to remove files older than X days

------------- Batch file ------------------------@echo off:: set folder pathset dump_path="E:\Shared\APP01\LOGFILES\RatingWebService2\Rating Comments\Production":: set min age of files…