-1

I want to fetch all attributes like only Attributes that have Values (like ADUC). Just I am able to getting attributes names. Also , I want to get all attributes together with values like below.

Script :

$ADUser = Get-ADUser -Identity 'user' -Properties *
$CusUser = New-Object -TypeName PSObject
$ADUser.PropertyNames | ?{$ADUser.$_ -ne $null} | %{ $CusUser | Add-Member -MemberType NoteProperty -Name $_ -Value $ADUser.$_ }

Output:

AccountExpirationDate
accountExpires
AccountLockoutTime
AccountNotDelegated
AllowReversiblePasswordEncryption
AuthenticationPolicy
AuthenticationPolicySilo
BadLogonCount
CannotChangePassword
CanonicalName
Certificates
City
CN
..
..
blah 
blah

My desired output:

AccountExpirationDate blank
accountExpires ; never
AccountLockoutTime ; blank
AccountNotDelegated ; blank
AllowReversiblePasswordEncryption ; blank
AuthenticationPolicy ; blank
AuthenticationPolicySilo ; blank
BadLogonCount ; blank
CannotChangePassword ; blank
Certificates ; blank
City ; blank
CN ; john T
..
..
blah 
blah
Arbelac
  • 1,698
  • 6
  • 37
  • 90
  • 1
    Output from _what_? `Add-Member` doesn't return any output by default – Mathias R. Jessen Oct 22 '20 at 14:48
  • 1
    You also say you want only attributes that have values but then your desired output shows blank. Which is it? – Doug Maurer Oct 22 '20 at 15:17
  • I have an answer to a similar question here: "How do I get properties that ONLY have populated values?" https://stackoverflow.com/questions/44368990/how-do-i-get-properties-that-only-have-populated-values/48551416#48551416 – js2010 Oct 22 '20 at 20:14

1 Answers1

1

You can do the following to output your ADUser object only showing properties with values:

$ADUser = Get-ADUser -Identity 'user' -Properties *
# $props contains property names (an array) with non-empty values
$props = $ADUser.PSObject.Properties | 
    Where {[string]$_.BaseObject -eq $ADUser.DistinguishedName -and ![string]::IsNullOrEmpty($_.Value)} |
        Select-Object -Expand Name
# Outputs user object with only properties that contain non-empty values.
$ADUser | Select-Object $Props

If you want to format the property/value pair output, you can do the following:

$ADUser = Get-ADUser -Identity 'user' -Properties *
# List all properties in format property ; value. Empty values show as string blank.
$ADUser.PSObject.Properties | 
    Where {[string]$_.BaseObject -eq $ADUser.DistinguishedName} | Foreach-Object {
        if ([string]::IsNullOrEmpty($_.Value)) {
            $value = 'blank'
        } else {
            $value = $_.Value
        }
        "{0} ; {1}" -f $_.Name,$value
    }
AdminOfThings
  • 23,946
  • 4
  • 17
  • 27
  • thanks for your reply. For Second script, I am getting `Microsoft.ActiveDirectory.Management.ADPropertyValueCollection` for some attributes such as `proxyAddresses`, `msExchDelegateListBL` Also I am getting `Select-Object : Cannot convert System.Management.Automation.PSObject to one of the following types {System.String, System.Management.Automation.ScriptBlock}` for your first script. – Arbelac Oct 23 '20 at 05:56