Tuesday 15 July 2008

Count-Hashtable.ps1

<#
.SYNOPSIS
    This script displays the count of items 
    in a hash table.
.DESCRIPTION
    This script creates a hash table then 
    prints out the number of items in the table.
.NOTES
    File Name  : 
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell Version 2.0
    Tested     : PowerShell Version 5
.LINK
    This script posted to:
        http://pshscripts.blogspot.co.uk/2008/07/count-hashtableps1.html
    
.EXAMPLE 
    PS c:\foo > .\count-hashtable.ps1
    There are 4 elements in $myht as follows:

    Name                   Value                                                                              
    ----                   -----                                                                             
    four                   fox
    one                    The    
    three                  brown  
    two                    quick
#>

# Create hashtable and add four elements
$myht = new-object system.collections.hashtable
$myht.Add( "one", "The" )
$myht.Add( "two", "quick" )
# or doing it the PowerShell way
$myht += @{"three" = "brown" }
$myht += @{"four" = "fox"}

# Display the Hashtable
"There are {0} elements in `$myht as follows:" -f $myht.count
$myht

No comments: