Skip to content

Instantly share code, notes, and snippets.

@snovvcrash
Forked from mattmcnabb/Set-GpoStatus.ps1
Created November 12, 2020 18:46
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 snovvcrash/ecdc639b061fe787617d8d92d8549801 to your computer and use it in GitHub Desktop.
Save snovvcrash/ecdc639b061fe787617d8d92d8549801 to your computer and use it in GitHub Desktop.
Function Set-GPOStatus
{
<#
.Synopsis
Set the status of a Group Policy Object
.Description
Sets the status of one or more Group Policy objects.
.Example
PS C:\> Get-Gpo MyGPO | Set-GPOStatus -Status AllSettingsEnabled
.Example
PS C:\> Set-GpoStatus MyGPO -Status ComputerSettingsDisabled -whatif
.Inputs
String or a Group Policy object
.Outputs
None
.Notes
Credit for this function goes to Jeff Hicks:
http://jdhitsolutions.com/blog/2013/02/set-gpo-status-with-powershell/
#######################################################################
# WARNING: This script has been tested function correctly in a lab environment.
# Use at your own risk and don't blame me if you break stuff!
#######################################################################
#>
[CmdletBinding(SupportsShouldProcess)]
Param
(
[Parameter(
Mandatory=$True,
ValueFromPipeline,
ValueFromPipelinebyPropertyName
)]
$DisplayName,
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateSet(
'AllSettingsEnabled',
'AllSettingsDisabled',
'ComputerSettingsDisabled',
'UserSettingsDisabled'
)]
$Status,
[string]
$Domain,
[string]
$Server
)
Begin
{
$Splat = @{ ErrorAction="Stop" }
if ($Domain) { $Splat.Add("Domain",$Domain) }
if ($Server) { $Splat.Add("Server",$Server) }
}
Process
{
if ($Displayname -is [string])
{
$Splat.Add("Name",$DisplayName)
Try { $Gpo = Get-GPO @Splat } Catch { $_; return }
}
else
{
$Splat.Add("GUID",$DisplayName.Id)
$Gpo = $DisplayName
}
if ($PSCmdlet.ShouldProcess("$($Gpo.Displayname) : $Status "))
{
$Gpo.GpoStatus = $Status
}
}
}
#requires -Version 3.0
#requires -Module GroupPolicy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment