Adding Variable to Hash Table.

How would I add $hashAdmin to Hash Table?

PS C:\Windows\system32> $hashadmin.gettype()

IsPublic IsSerial Name BaseType


True True String System.Array

PS C:\Windows\system32> $hashAdmin
Domain=“village”
Name=“catman”

$hashAdmin is apparently an array, not a hashtable, according to your output. What are you trying to do with it?

$ht = @{}
$ht.SomeArray = $hashadmin

You can add it just like any other element just as long as you assign it a key.

Would like to convert that variable to a object. I figured since it already in the correct hashtable format there should be a way to just add it.

$obj= New-object PSobject -property $hashadmin

But I think it still see it as an array.

I see. As luck would have it, your strings are in a format which makes it possible to use the ConvertFrom-StringData cmdlet (which will give you your hashtable):

$stringArray = [string[]]@(
    'Domain="village"'
    'Name="catman"'
)

# at this point, $stringArray is the same as the $hashAdmin variable
# from your original post.

# ConvertFrom-StringData expects a single string, not an array, so we
# join the array into multiple lines:

$string = $stringArray -join "`r`n"

$hashtable = ConvertFrom-StringData -StringData $string

# Now, you can create a psobject from the hashtable.

$object = New-Object psobject -Property $hashtable

$object

Below is the code so far. I would like to add the value of the $hashAdmin to a PSobject. Because the array is already in the correct key = value format . Simple said I want to convert it from an array to a hash table

$adminMember= Get-wmiobject win32_groupuser | where {$_.groupcomponent –like ‘*“Administrators”’}

$hash=$null
$hash=@{}

Foreach($admin in $adminMember ){

$groupComponent = $admin.Partcomponent

$hashAdmin= ($groupComponent -split “.”)[1] -split “,”

}

That worked perfectly.
output
Domain Name


“SecretComputername” “SecretUser”
“almosttheweekend” “Domain Admins”

  
$adminMember= Get-wmiobject win32_groupuser | where {$_.groupcomponent –like '*"Administrators"'}  

  

$hash=$null
$hash=@{}

Foreach($admin in $adminMember ){

$groupComponent = $admin.Partcomponent 


$hashAdmin= ($groupComponent -split "\.")[1] -split ","


$string= $hashAdmin -join "`r`n"

$hashtable = ConvertFrom-StringData -StringData $string
 
# Now, you can create a psobject from the hashtable.
 
$object = New-Object psobject -Property $hashtable
 
$object


}  

You can get that same information quite a bit more easily, if you like (without resorting to string parsing steps):

$group = Get-WmiObject Win32_Group -Filter 'Name = "Administrators"'
$group.GetRelated('Win32_UserAccount') | Select-Object -Property Domain, Name

Edit: I tested this on a home computer that’s not a member of a domain. For domain-joined computers, you may need to modify it slightly to make sure Get-WmiObject returns the correct group.

I need to check for local admin account membership on my system. For some reason the code is not exiting when complete. Any idea why it might not exit?

Thanks
Jon

If you’re in a domain environment, using WMI to check for group memberships (or anything account-related, for that matter) can take a ridiculously long time. I’d recommend using ADSI instead, or if you just want to display the information on screen, you can just run the old “net localgroup Administrators” command.