locked
PowerShell: how to populate hashtable automatically from get-aduser RRS feed

  • Question

  • using the following command, I will get the entire user detail from active directory. However, I want all the details populate into hashtable. How to do this.

    get-aduser User1234 -server myDomain -properties *

    So, if I use the following, I will get this:

    $table1 = get-aduser User1234 -server myDomain -properties *
    $table1.GetType()

    Result: BaseType is Microsoft.ActiveDirectory.Management.ADAccount

    It is not hashtable.

    Monday, March 27, 2017 5:56 PM

Answers

  • $user = Get-ADUser user1234 -Properties *
    $hash = @{}
    $user.PSObject.Properties|%{$hash.Add($_.Name, $_.Value)}
    $hash.GetType()
    $hash | Out-GridView -Title Hashtable

    • Marked as answer by WS03uhoh Monday, March 27, 2017 8:43 PM
    Monday, March 27, 2017 6:17 PM

All replies

  • Why would you want a hash table?  We use objects in PowerShell.


    \_(ツ)_/

    Monday, March 27, 2017 6:11 PM
  • $user = Get-ADUser user1234 -Properties *
    $hash = @{}
    $user.PSObject.Properties|%{$hash.Add($_.Name, $_.Value)}
    $hash.GetType()
    $hash | Out-GridView -Title Hashtable

    • Marked as answer by WS03uhoh Monday, March 27, 2017 8:43 PM
    Monday, March 27, 2017 6:17 PM
  • Thanks, Leif-Arne! It works!

    I made it one liner below:

    (Get-ADUser user1234 -Properties *).PSObject.Properties | foreach { $hash=@{} } {$hash.add($_.Name, $_.value) }

    Monday, March 27, 2017 8:46 PM