$templateName = "TemplateName" # Use CN, not display name
$principalName = "principal" # SAM account name of principal
# Find the certificate template
$rootDSE = New-Object DirectoryServices.DirectoryEntry("LDAP://RootDSE")
$template = [ADSI]"LDAP://CN=$templateName,CN=Certificate Templates,CN=Public Key Services,CN=Services,$($rootDSE.configurationNamingContext)"
# Construct the ACE
$objectTypeByteArray = [GUID]"0e10c968-78fb-11d2-90d4-00c04f79dc55"
$inheritedObjectTypeByteArray = [GUID]"00000000-0000-0000-0000-000000000000"
$account = New-Object System.Security.Principal.NTAccount($principalName)
$sid = $account.Translate([System.Security.Principal.SecurityIdentifier])
$ace = New-Object DirectoryServices.ActiveDirectoryAccessRule(
$sid,
[System.DirectoryServices.ActiveDirectoryRights]::ExtendedRight,
[System.Security.AccessControl.AccessControlType]::Allow,
$objectTypeByteArray,
[System.Security.AccessControl.InheritanceFlags]::None,
$inheritedObjectTypeByteArray
)
# Add the new ACE to the ACL
$acl = $template.psbase.ObjectSecurity
$acl.AddAccessRule($ace)
$template.psbase.CommitChanges()