Assuming you are running an AD CS Enterprise CA, certificate templates are stored in Active Directory, located in the Configuration NC.
(As noted by CryptoGuy in the comments, this approach is not supported by Microsoft - you really should just be using the Certificate Templates mmc, certtmpl.msc
, for this task)
To retrieve a certificate template:
$CertTemplateParams = @{
LDAPFilter = '(&(objectClass=pKICertificateTemplate))'
SearchBase = 'CN=Certificate Templates,CN=Public Key Services,CN=Services,{0}' -f ([adsi]'LDAP://RootDSE').configurationNamingContext[0]
Properties = 'pKIExpirationPeriod'
}
$Templates = Get-ADObject @CertTemplateParams
Filter the template you need:
$UserTemplate = $Templates |Where-Object { $_.Name -eq "User" }
The pKIExpirationPeriod attribute represents a 64-bit FILETIME struct, but you can convert it to a timespan with [BitConverter]::ToInt64()
:
# File time type counts in 100-nanosecond intervals, we need seconds
$Validity = New-TimeSpan -Seconds $([System.BitConverter]::ToInt64($UserTemplate.pKIExpirationPeriod, 0) * -.0000001)
Now add some time to the timespan:
$Validity.Add($(New-TimeSpan -Days 365))
Convert it back to a 64-bit byte array:
$NewExpirationPeriod = [System.BitConverter]::GetBytes($($Validity.TotalSeconds * -10000000))
Use Set-ADObject
to change the template object:
Set-ADObject -Identity $UserTemplate.objectGuid -Replace @{pKIExpirationPeriod = $NewExpirationPeriod}