<# .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
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Showing posts with label system.collections.hashtable. Show all posts
Showing posts with label system.collections.hashtable. Show all posts
Tuesday, 22 July 2008
Get-HashTable2.ps1
Labels:
add,
openwith,
powershell,
scripts,
system.collections.hashtable
Monday, 21 July 2008
Get-HashTable.ps1
<# .SYNOPSIS Sample using Hashtables .DESCRIPTION This script creates, populates and displays a hash table .NOTES File Name : Get-HashTable.ps1 Author : Thomas Lee - tfl@psp.co.ukequires : PowerShell V2 CTP3 .LINK http://www.pshscripts.blogspot.com .EXAMPLE PS C:\foo> Get-HashTable.ps1 The Hashtable contains the following: Name Value ---- ----- four fox two quick three brown one The #> $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. "The Hashtable contains the following:" $myht
Labels:
powershell,
system.collections.hashtable
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
Labels:
add,
powershell,
scripts,
system.collections.hashtable
Show-HashTableProperties.ps1
<# .SYNOPSIS Shows details of a hash table .DESCRIPTION This script creates, populates then displays a hash table. It then displays table properties, keys, values and does a synchroot. .NOTES File Name : Show-HashTableProperties.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 .LINK http://www.pshscripts.blogspot.com .EXAMPLE PS c:\foo> .\Show-HashTableProperties.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 Count of items in the hashtable : 4 Is hashtable fixed size? : False Is hashtable read-only? : False Is hashtabale synchronised? : False Keys in hashtable: bmp rtf txt dib Values in hashtable paint.exe wordpad.exe notepad.exe paint.exe $sr = System.Object #> ## # 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 "" # Display Properties "Count of items in the hashtable : {0}" -f $openwith.Count "Is hashtable fixed size? : {0}" -f $openwith.IsFixedSize "Is hashtable read-only? : {0}" -f $openwith.IsReadonly "Is hashtabale synchronised? : {0}" -f $openwith.IsSynchronized "";"Keys in hashtable:" $openwith.keys "";"Values in hashtable" $openwith.values "" # finally get syncroot (not much there!) $sr = $openwith.syncroot "`$sr = {0}" -f $sr
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
Labels:
add,
powershell,
scripts,
system.collections.hashtable
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
Labels:
add,
count,
powershell,
scripts,
system.collections.hashtable
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
Labels:
add,
powershell,
scripts,
system.collections.hashtable
Sunday, 13 July 2008
Clear-Hashtable.ps1
<# .SYNOPSIS Demonstrates Hashtable's Clear Method .DESCRIPTION This script creates and populates a hash table then demonstrates use of the .clear method. .NOTES File Name : Clear-HashTable.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 CTP3 .LINK http://www.pshscripts.blogspot.com .EXAMPLE PS C:\foo> .\ClearHashTable.ps1 Initially, Count : 5 Values: Name Value ---- ----- five jumped one The two quick three brown four fox After Clear, Count : 0 Values: #> ## # Start of Script ## # Create and populate a hash table $MyHashTable= @{} $MyHashTable.Add( "one", "The" ) $MyHashTable.Add( "two", "quick" ) $MyHashTable.Add( "three", "brown" ) # Add using the PowerShell way: :-) $MyHashTable += @{"four"="fox" } $myHashTable += @{"five"="jumped"} # Display the count and values of the Hashtable "Initially," " Count : {0}" -f $MyHashTable.Count " Values:" $MyHashTable # Clear the Hashtable. $MyHashTable.Clear() "" # Display the count and values of the Hashtable "After Clear," " Count : {0}" -f $MyHashTable.Count " Values:" $MyHashtable # End of script
Labels:
clear,
powershell,
scripts,
system.collections.hashtable
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
Labels:
add,
powershell,
scripts,
system.collections.hashtable
Show-QueueProperties.ps1
<# .SYNOPSIS Demonstrates using a queue object .DESCRIPTION This script creates a queue, then adds some items to it, print out the contents then synchs it. .NOTES File Name : show-queueproperties.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 Tags : showqueue,enqueue,synchronize,system.collections.queue .LINK http://www.pshscripts.blogspot.com .EXAMPLE PS c:\foo> .\show-queueproperties.ps1 Creating and populating $myq object with... 3 entries in the queue as follows: entry 0: Hello entry 1: World entry 2: Jerry Garcia Rocks!! Report on $myq $myq is synchronised? :False Synchronising $myq to $sq $sq is synchronised? :True #> ## # Start of script ## # Create new queue object $myq = new-object System.Collections.Queue $myq.Enqueue("Hello") $myq.Enqueue("World") # or a more powershell way $myq += "Jerry Garcia Rocks!!" # Show use of the properties of the Queue # First use count property "Creating and populating `$myq object with..." $i=0 "`{0} entries in the queue as follows:" -f $myq.Count $myq | % {"entry {0}: {1}" -f $i++,$_ } "" # Now check synchronisation "Report on `$myq" "`$myq is synchronised? :{0}" -f $myq.IsSynchronized "" # Synch the queue "Synchronising `$myq to `$sq" $sq = [system.collections.queue]::synchronized($myq) # report again "`$sq is synchronised? :{0}" -f $sq.IsSynchronized # End Script
Labels:
powershell,
scripts,
system.collections.hashtable
Subscribe to:
Posts (Atom)