I have had some trouble getting @Ob1lan's script to work out of the box because in order for it to work (i.e. to allow a script to elevate itself), the Powershell Execution Policy has to allow that. One way to do it is to set it globally using Set-ExecutionPolicy
. My preference is, though, to leave the global policy untouched and bypass it whenever needed using the -ExecutionPolicy ByPass
argument.
So below is the slightly modified script as I am using it now to disable my touch screen. Apart from adding the bypass, I also added the -WindowStyle hidden
argument to prevent too many windows polluting my screen. (Hint: if the script doesn't work as desired, I recommend you first replace that argument with the -noexit
argument to see any error messages.)
# Get the ID and security principal of the current user account
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
# Check to see if we are currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole))
{
# We are running "as Administrator" - so change the title and background color to indicate this
$Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + " (Elevated)"
$Host.UI.RawUI.BackgroundColor = "DarkBlue"
clear-host
}
else
{
# We are not running "as Administrator" - so relaunch as administrator
# Create a new process object that starts PowerShell
$newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
# Specify the current script path and name as a parameter, hide window and bypass execution policy (in case it has not been disabled globally)
$newProcess.Arguments = '-ExecutionPolicy ByPass -WindowStyle hidden ' + $myInvocation.MyCommand.Definition + '" ';
# Here are the changes ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Indicate that the process should be elevated
$newProcess.Verb = "runas";
# Start the new process
[System.Diagnostics.Process]::Start($newProcess);
# Exit from the current, unelevated, process
exit
}
Get-PnpDevice | Where-Object {$_.FriendlyName -like '*touch screen*'} | Enable-PnpDevice -Confirm:$false
Get-PnpDevice -FriendlyName "Intel(R) Display Audio" | Disable-PnpDevice -confirm:$false; Get-PnpDevice -FriendlyName "Intel(R) Display Audio" | Enable-PnpDevice -confirm:$false
-- I put this in a.ps1
file, then a shortcut per stackoverflow.com/a/10137272/1168342 - I didn't quite figure out how make it run always as requesting admin, but right-clicking the shortcut allows that option. Not quite one gesture, but it probably can be done. Thanks!