Hash tables
Nice article on hashtables
$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
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;
}
}
}
|