Question:
I like to write a cmdlet “Convert-ToHashTable” which performs following task:
1 2 |
$HashTable = Import-Csv Table.csv | Convert-ToHashTable |
Import-csv places an array on the pipeline, how can i change it to a hashtable in my Convert-ToHashTable cmdlet? In the Process part of the cmdlet i can access the elements, but i don’t know how to change the type of the pipeline itself
Process
{
Write-Verbose “Process $($myinvocation.mycommand)”
$CurrentInput = $_
…
}
Is there a way to return a complete hashtable as the new pipeline or create a new pipeline with type hashtable?
Answer:
What do you plan to use as keys for hashtable? other than that, it should be pretty easy to do even with simple foreach-object:
1 2 3 4 5 6 7 8 |
Import-Csv Table.csv | Foreach-Object -begin { $Out = @{} } -process { $Out.Add('Thing you want to use as key',$_) } -end { $Out } |
Don’t see need for any “change pipeline type” magic, honestly…?