How can I cast an AD Attribute in a filter condition when calling Get-ADUser in PowerShell? How can I cast an AD Attribute in a filter condition when calling Get-ADUser in PowerShell? powershell powershell

How can I cast an AD Attribute in a filter condition when calling Get-ADUser in PowerShell?


To my knowledge it's not possible to cast attributes to a different type in an AD search string. Despite the scriptblock-like notation the argument to the parameter -Filter is essentially a query string.

What you can do is do the filtering via Where-Object after fetching the objects. That's not optimal (because your AD query will return more objects than it needs to), but in this case I don't see another way. Make sure, however, that you only move those parts of the filter to the Where-Object that won't work otherwise, so that Where-Object doesn't need to process all user objects.

Get-ADUser-Filter {extensionAttribute12 -notlike '*' -and enabled -eq $true -and whencreated -lt $30Days} ... |    Where-Object { [DateTime]extensionAttribute12 -le $30days } |    ...