Skip to content

Instantly share code, notes, and snippets.

@sakkke
Created July 11, 2021 03:02
Show Gist options
  • Save sakkke/bdfc920da76eee211195754c64642bea to your computer and use it in GitHub Desktop.
Save sakkke/bdfc920da76eee211195754c64642bea to your computer and use it in GitHub Desktop.
# https://superuser.com/a/532109
param([switch]$Elevated)
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Test-Admin) -eq $false) {
if ($elevated) {
# tried to elevate, did not work, aborting
} else {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}
# instance id of pnp device to toggle
$instanceId = ''
function Toggle-PnpDevice {
param(
[parameter(ValueFromPipeline)] $pnpDevice
)
if ($pnpDevice.Status -eq 'OK') {
$pnpDevice | Disable-PnpDevice -Confirm:$false
} else {
$pnpDevice | Enable-PnpDevice -Confirm:$false
}
}
Get-PnpDevice | ? { $_.InstanceId -eq $instanceId } | Toggle-PnpDevice
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment