Powershell - How to add and view a GPO description

To support me, you can subscribe to the channel, share and like the videos, disable your ad blocker or make a donation. Thank you!

Hello,

It can be interesting to follow the modifications of GPOs, to add comments and to integrate information which seems useful to you. It is possible to do this from the Group Policy Editor and view the comments from the Details tab of the GPO.
But you can also do it from Powershell.

The Powershell GroupPolicy module is required.

# Create a GPO and define a comment on creation
New-GPO -Name GPO1 -Comment 'Test comment'
# Display a comment
(Get-GPO -Name GPO1).description
# Create a GPO and define a comment on creation
(Get-GPO -Name GPO1).description='Test comment'
# In this syntax the comment overwrites previous comments

And it can be adapted into a Powershell function which allows you to add a comment while preserving the previous comments. Comments are displayed from the most recent to the oldest. Comments will take the following form comment date (user):
2024-07-25_13:20:35 Rename administrator account (Guillaume)
2024-07-24_10:24:49 Comment test (Guillaume)

PowerShell function to add comments in a GPO
function Add-GpoNoteGB {
[CmdletBinding()]
param (
[string]
$GpoName,
[string]
$Note
)
begin {
Import-Module -Name GroupPolicy
[string]$ActualNote = (Get-GPO -Name $GpoName).description
}
process {
if ( $null -ne $ActualNote) { $LineBreak = "`n"}
[string]$NewNote = "{3}{0:yyyy-MM-dd_HH:mm:ss} {2} ({1})`r" -f (get-date), $env:USERNAME, $Note,$LineBreak
[string]$FinalNote = $NewNote, $ActualNote -join "`n"
(Get-GPO -Name $GpoName).description=$FinalNote
}
}
$Gpo = 'U_taskmgr'
Add-GPONoteGB -GpoName $Gpo -Note 'Comment test'
# Display comments
(Get-GPO -Name $Gpo).description

Related links