Showing posts with label add. Show all posts
Showing posts with label add. Show all posts

Tuesday, 22 July 2008

Get-HashTable2.ps1

<#
.SYNOPSIS
    A rich example of using hash tables with PowerShell  
.DESCRIPTION
    This script creates, populates a hash table then
    operates on the hash table
.NOTES
    File Name  : Get-HashTable2.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2
.LINK
    http://pshscripts.blogspot.co.uk/2008/07/get-hashtable2ps1_22.html
    http://msdn.microsoft.com/en-gb/netframework/aa663309.aspx
.EXAMPLE
    PS c:\foo> Get-HashTable2.ps1
    The Hash Table so far

    Name                           Value
    ----                           -----
    bmp                            paint.exe
    rtf                            wordpad.exe
    txt                            notepad.exe
    dib                            paint.exe

    Trying to add an entry with an existing key
   ***
   *** An element with Key = "txt" already exists.
   ***
   Continuing

    for key = "rtf", value = wordpad.exe.
    for key = "rtf", value = winword.exe.

    The Hash Table so far
    doc                            winword.exe
    bmp                            paint.exe
    rtf                            winword.exe
    txt                            notepad.exe
    dib                            paint.exe

    Value added for key = "ht": hypertrm.exe

    6 elements in the hash table as follows:
    doc                            winword.exe
    bmp                            paint.exe
    ht                             hypertrm.exe
    rtf                            winword.exe
    txt                            notepad.exe
    dib                            paint.exe

    Value Entries:
    Value = winword.exe
    Value = paint.exe
    Value = hypertrm.exe
    Value = winword.exe
    Value = notepad.exe
    Value = paint.exe

    Key Entries
    Key = doc
    Key = bmp
    Key = ht
    Key = rtf
    Key = txt
    Key = dib
    
    
    Removing("doc")
    Key "doc" is no longer found.
#>

###
#  Start of script
##

# create new hash table
$openWith = @{}
 
# Add some elements to the hash table. There are no 
# duplicate keys, but some of the values are duplicates.
$openWith.Add('txt', 'notepad.exe')
$openWith.Add('dib', 'paint.exe')
 
# Add in a powershell way!
$openwith += @{'bmp'='paint.exe'}
# or
$wordpad  =  'wordpad.exe'
$openwith += @{'rtf'=$wordpad}
 
# Display the Hash table
'The Hash Table so far'
$openwith
 
# The Add method throws an exception if the new key is 
# already in the hash table.
  
#first catch exception
 trap [Exception] {
    '***' 
    write-host "*** An element with Key = `"txt`" already exists."
    '***'
    continue
 }
 
# now the offending line.
'';'Trying to add an entry with an existing key'
$openWith.Add('txt', 'winword.exe');
'Continuing';''
 
# The Item property is the default property, so you 
# can omit its name when accessing elements
# A non-existing key comes up empty
"For key = `"rtf`", value = {0}." -f $openWith['rtf']
 
# The default Item property can be used to change the value
# associated with a key.
# add an entry for RTF 
 $openWith['rtf'] = 'winword.exe';
 "For key = `"rtf`", value = {0}." -f $openWith['rtf']
    
# If a key does not exist, setting the default Item property
# for that key adds a new key/value pair.
$openWith['doc'] = 'winword.exe'
 
# Note Progress with this hashtable
'';'The Hash Table so far'
$openwith
''
 
# The ContainsKey method can be used to test keys before inserting them.
# tesst the "ht" key before adding it into the hashtable
if (!$openWith.ContainsKey('ht')) {
            $openWith.Add('ht', 'hypertrm.exe')
            "Value added for key = `"ht`": {0}" -f $openWith['ht']
}
 

# When you use foreach to enumerate hash table elements,
# the elements are retrieved as KeyValuePair objects.
''
 
'{0} elements in the hash table as follows:' -f $openwith.count 
$openwith
 
# To get the values alone, use the Values property.
$valueColl = $openWith.Values
 
# To get the values alone, use the Values property.
$valueColl = $openWith.Values
 
# The elements of the ValueCollection are strongly typed
#with the type that was specified for hash table values.
'';'Value Entries:'
foreach( $s in $valueColl ) {
            'Value = {0}' -f $s
}
 
# To get the keys alone, use the Keys property.
$keyColl = $openWith.Keys
 
# The elements of the KeyCollection are strongly typed
# with the type that was specified for hash table keys.
'';'Key Entries'
foreach( $s in $keyColl ){
        'Key = {0}' -f $s
}
 
# Use the Remove method to remove a key/value pair.
"`nRemoving(`"doc`")"
$openWith.Remove('doc')
# See if it's there
if (!$openWith.ContainsKey('doc')) {
           "Key `"doc`" is no longer found."
 }
# End of script

Saturday, 19 July 2008

Synch-HashTable.ps1

<#
.SYNOPSIS
    Demonstrates Hashtable IsSynchronized 
.DESCRIPTION
    This script creates and populates a hash table, then
    checks on and displays sync status.
.NOTES
    File Name  : Sync-HashTable.ps1A
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2 
.LINK
    http://pshscripts.blogspot.co.uk/2008/07/synch-hashtableps1.html
.EXAMPLE
    PS c:\foo> Sync-HashTable.ps1
    $myht is synchronised      : False
    $mySyncdht is synchronised : True
#>

##
#  Start of script
##

# Create and initialise a new Hashtable
$myht = new-object system.collections.hashtable
$myht.Add( 0, "zero")
$myht.Add( 1, "one")
$myht.Add( 2, "two")
$myht.Add( 3, "three")
# or
$myht += @{"4"="four"}

# Create a synchronized wrapper around the Hashtable.
$mysyncdht = [system.collections.Hashtable]::Synchronized( $myht)

# Display the sychronization status of both Hashtables
"`$myht is synchronised      : {0}" -f $myht.IsSynchronized
"`$mySyncdht is synchronised : {0}" -f $mysyncdht.IsSynchronized
# End of script

Thursday, 17 July 2008

Show-HashTableValues.ps1

<#
.SYNOPSIS
    Shows use of Hash tables with PowerSHell
.DESCRIPTION
    This script creates and populates a Hash Table then displays the values, keys, etc.
.NOTES
    File Name  : Show-HashtableValues.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2
.LINK
    http://pshscripts.blogspot.co.uk/2008/07/show-hashtablevaluesps1.html
.EXAMPLE
    PSH [C:\foo]: .\show-hashtablevalues.ps1
    The Hash Table so far

   Name                           Value
   ----                           -----
   bmp                            paint.exe
   rtf                            wordpad.exe
   txt                            notepad.exe
   dib                            paint.exe

   4 Keys in this table as follows:
   1:  bmp
   2:  rtf
   3:  txt
   4:  dib

   4 Values in this table as follows:
   1:  paint.exe
   2:  wordpad.exe
   3:  notepad.exe
   4:  paint.exe
#>

##
# Start of Script
##


# Create new hash table
$openWith = new-object system.collections.hashtable

# Add some elements to the hash table. 
$openWith.Add('txt', 'notepad.exe')
$openWith.Add('dib', 'paint.exe')

# Add more entries in a more Powershell way!
$openwith += @{'bmp'='paint.exe'}
# or
$wordpad  =    'wordpad.exe'
$openwith += @{'rtf'=$wordpad}

# Display the Hash table
'The Hash Table so far'
$openwith
''

# To get the keys alone, use the Keys property
$i=0
'{0} Keys in this table as follows:' -f $openwith.keys.count
$openwith.keys | %{'{0}:  {1}' -f ++$i, $_}
''

# To get the values alone, use the Values property
$i=0
'{0} Values in this table as follows:' -f $openwith.values.count
$openwith.values | %{'{0}:  {1}' -f ++$i, $_}
''
# End of Script

Wednesday, 16 July 2008

Get-Hashtable2.ps1

<#
.SYNOPSIS
    A rich example of using hash tables with PowerShell  
.DESCRIPTION
    This script creates, populates a hash table then
    operates on the hash table
.NOTES
    File Name  : Get-HashTable2.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2 CTP3
.LINK
    http://www.pshscripts.blogspot.com
    http://msdn.microsoft.com/en-gb/netframework/aa663309.aspx
.EXAMPLE
    PS c:\foo> Get-HashTable2.ps1
    The Hash Table so far

    Name                           Value
    ----                           -----
    bmp                            paint.exe
    rtf                            wordpad.exe
    txt                            notepad.exe
    dib                            paint.exe

    Trying to add an entry with an existing key
   ***
   *** An element with Key = "txt" already exists.
   ***
   Continuing

    for key = "rtf", value = wordpad.exe.
    for key = "rtf", value = winword.exe.

    The Hash Table so far
    doc                            winword.exe
    bmp                            paint.exe
    rtf                            winword.exe
    txt                            notepad.exe
    dib                            paint.exe

    Value added for key = "ht": hypertrm.exe

    6 elements in the hash table as follows:
    doc                            winword.exe
    bmp                            paint.exe
    ht                             hypertrm.exe
    rtf                            winword.exe
    txt                            notepad.exe
    dib                            paint.exe

    Value Entries:
    Value = winword.exe
    Value = paint.exe
    Value = hypertrm.exe
    Value = winword.exe
    Value = notepad.exe
    Value = paint.exe

    Key Entries
    Key = doc
    Key = bmp
    Key = ht
    Key = rtf
    Key = txt
    Key = dib
    
    
    Removing("doc")
    Key "doc" is no longer found.
#>

###
#  Start of script
##

# create new hash table
$openWith = @{}
 
# Add some elements to the hash table. There are no 
# duplicate keys, but some of the values are duplicates.
$openWith.Add('txt', 'notepad.exe')
$openWith.Add('dib', 'paint.exe')
 
# Add in a powershell way!
$openwith += @{'bmp'='paint.exe'}
# or
$wordpad  =  'wordpad.exe'
$openwith += @{'rtf'=$wordpad}
 
# Display the Hash table
'The Hash Table so far'
$openwith
 
# The Add method throws an exception if the new key is 
# already in the hash table.
  
#first catch exception
 trap [Exception] {
    '***' 
    write-host "*** An element with Key = `"txt`" already exists."
    '***'
    continue
 }
 
# now the offending line.
'';'Trying to add an entry with an existing key'
$openWith.Add('txt', 'winword.exe');
'Continuing';''
 
# The Item property is the default property, so you 
# can omit its name when accessing elements
# A non-existing key comes up empty
"For key = `"rtf`", value = {0}." -f $openWith['rtf']
 
# The default Item property can be used to change the value
# associated with a key.
# add an entry for RTF 
 $openWith['rtf'] = 'winword.exe';
 "For key = `"rtf`", value = {0}." -f $openWith['rtf']
    
# If a key does not exist, setting the default Item property
# for that key adds a new key/value pair.
$openWith['doc'] = 'winword.exe'
 
# Note Progress with this hashtable
'';'The Hash Table so far'
$openwith
''
 
# The ContainsKey method can be used to test keys before inserting them.
# tesst the "ht" key before adding it into the hashtable
if (!$openWith.ContainsKey('ht')) {
            $openWith.Add('ht', 'hypertrm.exe')
            "Value added for key = `"ht`": {0}" -f $openWith['ht']
}
 

# When you use foreach to enumerate hash table elements,
# the elements are retrieved as KeyValuePair objects.
''
 
'{0} elements in the hash table as follows:' -f $openwith.count 
$openwith
 
# To get the values alone, use the Values property.
$valueColl = $openWith.Values
 
# To get the values alone, use the Values property.
$valueColl = $openWith.Values
 
# The elements of the ValueCollection are strongly typed
#with the type that was specified for hash table values.
'';'Value Entries:'
foreach( $s in $valueColl ) {
            'Value = {0}' -f $s
}
 
# To get the keys alone, use the Keys property.
$keyColl = $openWith.Keys
 
# The elements of the KeyCollection are strongly typed
# with the type that was specified for hash table keys.
'';'Key Entries'
foreach( $s in $keyColl ){
        'Key = {0}' -f $s
}
 
# Use the Remove method to remove a key/value pair.
"`nRemoving(`"doc`")"
$openWith.Remove('doc')
# See if it's there
if (!$openWith.ContainsKey('doc')) {
           "Key `"doc`" is no longer found."
 }
# End of script

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

Monday, 14 July 2008

Contains-Hashtable.ps1

<#
.SYNOPSIS
    Hashtable Contains method sample using PowerShell 
.DESCRIPTION
    This script create and populates a hash table then demonstrates
    use of contains, containskey and containsvalue methods.
.NOTES
    File Name  : Contains-HashTable.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2 CTP3
.LINK
    http://pshscripts.blogspot.co.uk/2008/07/contains-hashtableps1.html
.EXAMPLE
    PS c:\foo> .\Contains-HashTable.ps1
	The Hashtable contains the following values:
    
    Name                           Value
    ----                           -----
    4                              four
    0                              zero
    2                              two
    1                              one
    3                              three

    The key "0" is in the hashtable: True
    The key "5" is in the hashtable: False

    The key "2" is in the hashtable: True
    The key "6" is in the hashtable: False

    The value "three" is in the hash table: True
    The value "nine" is in the hash table: False
#>

##
#  Start of script
##

$myht = @{}
$myht.Add( 0, 'zero' );
$myht.Add( 1, 'one' );
$myht.Add( 2, 'two' );
# or a more powershell way
$myht += @{'3'= 'three'}
$myht += @{'4'='four' }

# Display the values of the Hashtable
'The Hashtable contains the following values:'
$myht
''

# use contains method
$myKey = 0;
"The key `"{0}`" is in the hashtable: {1}" -f $myKey, $myHT.Contains( $myKey )
$myKey = 5;
"The key `"{0}`" is in the hashtable: {1}" -f $myKey, $myHT.Contains( $myKey )
''

# Now use ContainsKey
$myKey = 2;
"The key `"{0}`" is in the hashtable: {1}" -f $myKey, $myHT.ContainsKey( $myKey )
$myKey = 6;
"The key `"{0}`" is in the hashtable: {1}" -f $myKey, $myHT.ContainsKey( $myKey )
''

# Searches for a specific value using ContainsValue
$myValue = 'three';
"The value `"{0}`" is in the hash table: {1}" -f $myValue, $myHT.ContainsValue( $myValue )
$myValue = 'nine';
"The value `"{0}`" is in the hash table: {1}" -f $myValue, $myHT.ContainsValue( $myValue )
# End of script

Clone-Hashtable.ps1

<#
.SYNOPSIS
    Demonstrates the use of the .clone method with hash tables    
.DESCRIPTION
    This script creates and populates a hash table, then demosmtrates
    the use of the .clone() method.
.NOTES
    File Name  : Clone-HashTable.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2
.LINK
    http://www.pshscripts.blogspot.com
.EXAMPLE
    PS C:\foo> .\Clone-HashTable.ps1
	There are 4 in the $openwith hash table as follows

    Name                           Value
    ----                           -----
    bmp                            paint.exe
    rtf                            wordpad.exe
    txt                            notepad.exe
    dib                            paint.exe

    There are 4 in the $newtable hash table as follows
    bmp                            paint.exe
    rtf                            wordpad.exe
    txt                            notepad.exe
    dib                            paint.exe
#>

##
# start of script
##

# create new hash table
$openWith = @{}

# Add some elements to the hash table. 
$openWith.Add("txt", "notepad.exe")
$openWith.Add("dib", "paint.exe")
# Add in a powershell way!
$openwith += @{"bmp"="paint.exe"}
# or
$wordpad  =    "wordpad.exe"
$openwith += @{"rtf"=$wordpad}

# display hash table
"There are {0} in the `$openwith hash table as follows:" -f $openwith.count
$openwith
""

# now clone the table
$newtable = $openwith.clone()

# display new hash table
"There are {0} in the `$newtable hash table as follows:" -f $newtable.count
$newtable
# End of script

Saturday, 12 July 2008

Add-Hashtable.ps1

<#
.SYNOPSIS
    Demonstrates adding items to a hashtable  
.DESCRIPTION
    This script creates a Hashtable, then adds some items to it, and 
    finally prints out the resulting hash table.
.NOTES
    File Name  : Add-Hashtable.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2
.LINK
    http://www.pshscripts.blogspot.com
.EXAMPLE
    PS c:\foo> .\Add-Hashtable.ps1
     The Hashtable contains the following:

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

##
#  Start of script
##

# Create a hash table
$MyHashTable = @{}

# Add to it using .NET
$MyHashTable.Add( 'one', 'The' )
$MyHashTable.Add( 'two', 'quick')

# Add the PowerShell Way:
$MyHashTable += @{'three' = 'brown'}
$MyHashTable += @{'four'  = 'fox'  }

# Display the Hashtable.
' The Hashtable contains the following:' 
$MyHashTable

Friday, 4 July 2008

Get-SortedList.ps1

<#
.SYNOPSIS
    Sorted list sample
.DESCRIPTION
     This script creates then manipulates a sorted list
.NOTES
    File Name  : Get-SortedList.ps1
   Author     : Thomas Lee - tfl@psp.co.uk
   Requires   : PowerShell V2 CTP3
.LINK
    http://pshscripts.blogspot.com/2008/07/get-sortedlistps1.html
.EXAMPLE
    PSH [C:\foo]: .\Get-SortedList.ps1
    $mySL
      Count:    3
      Capacity: 16
      Keys and Values:
            -KEY-   -VALUE-
            First:  Hello
            Second: World
            Third:  !

    $mySL after two additions
      Count:    5
      Capacity: 16
      Keys and Values:
            -KEY-   -VALUE-
            AAAA:   aaaa
            First:  Hello
            Second: World
            Third:  !
            ZZZZ:   zzzz
#>

##
# Start of script
##
 
# Define Helper Function
function PrintKeysAndValues( $myList )  {
"`t-KEY-`t-VALUE-" 
for ( [int] $i = 0; $i -lt $myList.Count; $i++ )  {
    "`t{0}:`t{1}" -F $myList.GetKey($i), $myList.GetByIndex($i)
      }
""
}
 
# Create and initialise a new SortedList object
$mySL = new-object system.collections.SortedList
$mySL.Add("First", "Hello")
$mySL.Add("Second", "World")
$mySL.Add("Third", "!")

# Display the properties and values of the SortedList
"`$mySL" 
"  Count:    {0}" -f $mySL.Count
"  Capacity: {0}" -f $mySL.Capacity
"  Keys and Values:"
PrintKeysAndValues( $mySL )
 
# Add two more and display results
$mysl.add("AAAA",  "aaaa")
$mysl.add("ZZZZ" , "zzzz")
 
# display results
"`$mySL after two additions" 
"  Count:    {0}" -f $mySL.Count
"  Capacity: {0}" -f $mySL.Capacity
"  Keys and Values:"
PrintKeysAndValues( $mySL )
# End of script