hashtable

Hash tables

Nice article on hashtables

http://mjolinor.wordpress.com/2012/01/29/powershell-hash-tables-as-accumulators/

http://blogs.technet.com/b/heyscriptingguy/archive/2011/01/17/simplify-powershell-hash-table-creation-with-a-simple-cmdlet.aspx

$hash = @{} get-childitem $home -recurse | foreach-object {$hash[$_.extension]++} $hash

get-help -full about_hash_tables | less  #import hashtable ConvertFrom-StringData

Object Types in HashTables

    The keys and values in a hash table can have any .NET object type,

    and a single hash table can have keys and values of multiple types.

$p = @{"PowerShell" = (get-process PowerShell);     "Notepad" = (get-process notepad)} $p = $p + @{(get-service winrm) = ((get-service winrm).status)} $p $p.keys

$SourcesHT = @{} Foreach ($source in $SourcesTBL) {     $SourcesHT.Add($Source.id,$Source.Name) }

Convert to Hashtable or pscustomobject

http://blogs.msdn.com/b/timid/archive/2013/03/05/converting-pscustomobject-to-from-hashtables.aspx

 function ConvertTo-PsCustomObjectFromHashtable {       param (           [Parameter(                Position = 0,                 Mandatory = $true,                 ValueFromPipeline = $true,                ValueFromPipelineByPropertyName = $true            )] [object[]]$hashtable       );             begin { $i = 0; }             process {           foreach ($myHashtable in $hashtable) {               if ($myHashtable.GetType().Name -eq 'hashtable') {                   $output = New-Object -TypeName PsObject;                   Add-Member -InputObject $output -MemberType ScriptMethod -Name AddNote -Value {                        Add-Member -InputObject $this -MemberType NoteProperty -Name $args[0] -Value $args[1];                   };                   $myHashtable.Keys | Sort-Object | % {                        $output.AddNote($_, $myHashtable.$_);                    }                   $output;               } else {                   Write-Warning "Index $i is not of type [hashtable]";               }               $i += 1;            }       }  }   function ConvertTo-HashtableFromPsCustomObject {       param (           [Parameter(                Position = 0,                 Mandatory = $true,                 ValueFromPipeline = $true,                ValueFromPipelineByPropertyName = $true            )] [object[]]$psCustomObject       );             process {           foreach ($myPsObject in $psCustomObject) {               $output = @{};               $myPsObject | Get-Member -MemberType *Property | % {                   $output.($_.name) = $myPsObject.($_.name);               }               $output;           }       }  }