Skip to content

Instantly share code, notes, and snippets.

@tom-henderson
Created March 1, 2017 03:28
Show Gist options
  • Save tom-henderson/cbae6cc2d55514b6f43541e8af1037d4 to your computer and use it in GitHub Desktop.
Save tom-henderson/cbae6cc2d55514b6f43541e8af1037d4 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Set the state of a basic GPO setting
.DESCRIPTION
A Basic GPO setting has three possible states:
- Not Configured: The value is not present in the registry
- Enabled: The REG-DWORD is set to 0x00000001 (1)
- Disabled: The REG-DWORD is set to 0x00000000 (0)
.EXAMPLE
Disable RDP password saving
Set-GPRegistryValueState -Name 'GPO Name' -Key 'HKLM\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services' -ValueName 'DisablePasswordSaving' -State Enabled
#>
Function Set-GPRegistryValueState {
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[string] $Name,
[Parameter(Mandatory=$true)]
[string] $Key,
[Parameter(Mandatory=$true)]
[string] $ValueName,
[Parameter(Mandatory=$true)]
[ValidateSet("NotConfigured","Enabled","Disabled")]
[string] $State
)
Switch ($State) {
"NotConfigured" {
try {
Remove-GPRegistryValue -Name $Name -Key $Key -ValueName $ValueName -ErrorAction Stop
} catch [System.ArgumentException] {
Write-Verbose "$Key $ValueName is already not configured."
}
}
"Enabled" {
Set-GPRegistryValue -Name $Name -Key $Key -ValueName $ValueName -Type DWORD -Value 1
}
"Disabled" {
Set-GPRegistryValue -Name $Name -Key $Key -ValueName $ValueName -Type DWORD -Value 0
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment