Skip to content

Instantly share code, notes, and snippets.

@megamorf
Last active November 28, 2015 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save megamorf/68bda7e881cb3a6bc394 to your computer and use it in GitHub Desktop.
Save megamorf/68bda7e881cb3a6bc394 to your computer and use it in GitHub Desktop.
Query UAC Status (http://www.ehloworld.com/1026)
function Get-UACStatus {
<#
.SYNOPSIS
Gets the current status of User Account Control (UAC) on a computer.
.DESCRIPTION
Gets the current status of User Account Control (UAC) on a computer. $true indicates UAC is enabled, $false that it is disabled.
.NOTES
Version : 1.0
Rights Required : Local admin on server
: ExecutionPolicy of RemoteSigned or Unrestricted
Author(s) : Pat Richard (pat@innervation.com)
Dedicated Post : http://www.ehloworld.com/1026
Disclaimer : You running this script means you won't blame me if this breaks your stuff.
.EXAMPLE
Get-UACStatus
Description
-----------
Returns the status of UAC for the local computer. $true if UAC is enabled, $false if disabled.
.EXAMPLE
Get-UACStatus -Computername mycomputer
Description
-----------
Returns the status of UAC for the computer specified via -Computer. $true if UAC is enabled, $false if disabled.
.LINK
http://www.ehloworld.com/1026
#Requires -Version 2.0
#>
param(
[parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $false)]
[Alias("Computer","__Server","IPAddress","CN","dnshostname")]
[string[]]$Computername = $env:COMPUTERNAME
)
BEGIN {
$RegistryValue = "EnableLUA"
$RegistryPath = "Software\Microsoft\Windows\CurrentVersion\Policies\System"
$UACStatus = $false
}
PROCESS {
foreach($Computer in $Computername)
{
try {
$UACStatus = $Null
$OpenRegistry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$Computer)
$Subkey = $OpenRegistry.OpenSubKey($RegistryPath,$false)
$UACStatus = [bool]($Subkey.GetValue($RegistryValue))
}
catch [System.Management.Automation.MethodInvocationException] {
Write-Warning -Message "Error accessing [$Computer] - host is down or does not exist"
}
Catch [System.Security.SecurityException] {
Write-Warning -Message "Error accessing [$Computer] - please verify access permissions"
}
finally {
New-Object psobject -Property @{
Computername = $Computer
UACEnabled = $UACStatus
}
}
}
}
} # end function Get-UACStatus
Get-UACStatus
$env:COMPUTERNAME,"doesnotexist" | Get-UACStatus
Get-WmiObject win32_operatingsystem | Get-UACStatus
Get-UACStatus -Computername $env:COMPUTERNAME,"doesnotexist"
function Get-UACStatus {
<#
.SYNOPSIS
Gets the current status of User Account Control (UAC) on a computer.
.DESCRIPTION
Gets the current status of User Account Control (UAC) on a computer. $true indicates UAC is enabled, $false that it is disabled.
.NOTES
Version : 1.0
Rights Required : Local admin on server
: ExecutionPolicy of RemoteSigned or Unrestricted
Author(s) : Pat Richard (pat@innervation.com)
Dedicated Post : http://www.ehloworld.com/1026
Disclaimer : You running this script means you won't blame me if this breaks your stuff.
.EXAMPLE
Get-UACStatus
Description
-----------
Returns the status of UAC for the local computer. $true if UAC is enabled, $false if disabled.
.EXAMPLE
Get-UACStatus -Computername mycomputer
Description
-----------
Returns the status of UAC for the computer specified via -Computer. $true if UAC is enabled, $false if disabled.
.LINK
http://www.ehloworld.com/1026
#Requires -Version 2.0
#>
param(
[parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $false)]
[Alias("Computer","__Server","IPAddress","CN","dnshostname")]
[string[]]$Computername = $env:COMPUTERNAME
)
PROCESS {
foreach($Computer in $Computername)
{
try {
$UACStatus = [bool](Invoke-Command -ComputerName $Computer -ScriptBlock {
(Get-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\System' -Name 'EnableLUA').EnableLUA
} -ErrorAction Stop)
}
catch {
Write-Warning -Message "Error accessing [$Computer] - host might is down or does not exist"
$UACStatus = $Null
}
finally {
New-Object psobject -Property @{
Computername = $Computer
UACEnabled = $UACStatus
}
}
}
}
} # end function Get-UACStatus
Get-UACStatus
$env:COMPUTERNAME,"doesnotexist" | Get-UACStatus
Get-WmiObject win32_operatingsystem | Get-UACStatus
Get-UACStatus -Computername $env:COMPUTERNAME,"doesnotexist"
Get-UACStatus | where{$_.UACEnabled -eq $true} | Set-UACStatus -Enabled:$false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment