Home > OS >  Hashtable to PSCustomObject issue
Hashtable to PSCustomObject issue

Time:09-02

Not sure where I am going wrong with this (technique taken from Formatting .CSV in Powershell).
Clothes.txt contains the following:

colour blue
size large
material velvet
label red
colour pink
size small
material cotton
label blue
colour green
size medium
material plastic
label purple
colour gold
size medium
material silk

I am using the following code to search clothes.txt for occurrences of the words: colour,size or material, and if found to add it as a key to a hashtable ($hash) with a value of the word that follows it in the list.

Set-Location d:\temp
Get-ChildItem .\clothes.txt |
ForEach-Object {
    $hash = [ordered] @{}
    $_ |
    Select-String -Pattern '(colour|size|material) (.*)' |
    ForEach-Object { $hash[$_.Matches[0].Groups[1]] = $_.Matches[0].Groups[2] }
    [PSCustomObject]$hash
}

when I enter $hash it appears to show me the correct data

PS D:\temp> $hash

Name                           Value                                                                                                                                                          
----                           -----                                                                                                                                                          
colour                         blue                                                                                                                                                           
size                           large                                                                                                                                                          
material                       velvet                                                                                                                                                         
colour                         pink                                                                                                                                                           
size                           small                                                                                                                                                          
material                       cotton                                                                                                                                                         
colour                         green                                                                                                                                                          
size                           medium                                                                                                                                                         
material                       plastic                                                                                                                                                        
colour                         gold                                                                                                                                                           
size                           medium                                                                                                                                                         
material                       silk          
 

However when I try to convert the hash table to a pscustomobject using [pscustomobject]$hash I get only one result

PS D:\temp> [pscustomobject]$hash

colour size   material
------ ----   --------
gold   medium silk  

What I was expecting was all colours in $hash to be under the colour heading, all sizes under size etc instead of just the last entry. I have tried all different approaches, loops, getenumerator() etc. How do I fix this?

CodePudding user response:

Here is a different way you could do it using Regex.Matches:

[regex]::Matches((Get-Content .\test.txt -Raw), '(colour|size|material) (\w )') | & {
    begin { $i = 0; $out = [ordered]@{} }
    process {
        $out[$_.Groups[1].Value] = $_.Groups[2].Value
        if($i   -and $i % 3 -eq 0) {
            [pscustomobject] $out
            $out.Clear()
        }
    }
}

Using the example provided in question the output would be:

colour size   material
------ ----   --------
blue   large  velvet
pink   small  cotton
green  medium plastic
gold   medium silk
  • Related