Wednesday 30 July 2008

Get-AssemblyVersion.ps1

<#
.SYNOPSIS
    Gets the version number for an assembly
.DESCRIPTION
    
.NOTES
    File Name  : Get-AssemblyVersion.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2 CTP3
.LINK
    http://pshscripts.blogspot.co.uk/2008/07/get-assemblyversionps1.html
.EXAMPLE
    PSH [C:\foo]: .\Get-AssemblyVersion.ps1'
    Assembly: System.Speech has version number of: 3.0.0.0    
#>

## 
# Start of script
##

# Define the assembly we want to load - a random reference assembly from SDK 3.0
$Pshfile = "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\system.speech.dll"

# Now load the assembly
$Myasm = [System.Reflection.Assembly]::Loadfile($Pshfile)

# Get name, version and display the results
$Aname = $Myasm.GetName()
$Aver =  $Aname.version

# Display results
"Assembly: {0} has version number of: {1}" -f $Aname.name, $aver
# End of script

Tuesday 29 July 2008

Get-MathPi.ps1

<#
.SYNOPSIS
    Shows use of PI field    
.DESCRIPTION
    This script gets and displays system.math's PI field
.NOTES
    File Name  : Get-MathPI.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell
.LINK
    http://www.pshscripts.blogspot.com
.EXAMPLE
    PSH [C:\foo]: .\get-mathpi.ps1
    The Value of Pi is 3.14159265358979
#>

##
# Start of script
##

# Obtain value of Pi 
$pi = [math]::pi

# Display the value
"The Value of Pi is {0}" -f $pi
# End of Script

Monday 28 July 2008

Get-MathE.ps1

<#
.SYNOPSIS
    Illustrates the exponential value 'e'
.DESCRIPTION
    This script uses system.math's E field
.NOTES
    File Name  : get-mathe.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2
.LINK
    http://pshscripts.blogspot.co.uk/2008/07/get-matheps1.html
.EXAMPLE
    PSH [C:\foo]: . \get-mathe.ps1'
    The Value of E is 2.71828182845905
#>

##
# Start of Script
##
 
# get the value
$e = [math]::e
 
# print it out
"The Value of E is {0}" -f $e
# End of Script

Saturday 26 July 2008

Get-GuidByteString.ps1

<#
.SYNOPSIS
    Creates and displays a GUID as a byte String
.DESCRIPTION
    This script first creates a guid, then displays it as
    a byte string
.NOTES
    File Name  : Get-GuidByteString.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2
.LINK
    http://pshscripts.blogspot.co.uk/2008/07/get-guidbybytestring.html
.EXAMPLE
    PSH [C:\foo]: .\Get-GuidByteString.ps1'
    The GUID: 85d9e3b9-b414-481a-a773-57ca98ae893c converted to byte array is:
    185
    227
    217
    133
    20
    180
    26
    72
    167
    115
    87
    202
    152
    174
    137
    60

#>
 
# Create a new GUID
$g = [system.guid]::newguid()

# Get guid into byte array
$b=$g.tobytearray()

# Finally display the byte array
'The GUID: {0} converted to byte array is:' -f $g
$b

Friday 25 July 2008

New-Guid.ps1

<#
.SYNOPSIS
    Gets a new guid
.DESCRIPTION
    This script used sytem.guid to create a new guiod
.NOTES
    File Name  : New-Guid
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell 
.LINK
    http://www.pshscripts.blogspot.com
.EXAMPLE
    PSH [C:\foo]: .\New-Guid.ps1
    Guid
----
8aadd649-9e6b-4f8c-882d-351636ab5ed4
#> ## # Start of Script ## # Create a new GUID $g = [system.guid]::newguid() # Display the new guid $g # End of script

Thursday 24 July 2008

Get-EmptyGuid.ps1

# Get-Empty Guid.ps1
#
Get's an empty GUID
#
thomas lee - tfl@psp.co.uk

# create an empty GUID
$g = [system.guid]::empty

# Display the empty guid
"The Empty guid: {0}" -f $g



This script produces the following output:



PS C:\Users\tfl> 
E:\PowerShellScriptLib\System.Guid\Get-EmptyGuid.ps1
The Empty guid: 00000000-0000-0000-0000-000000000000


Technorati Tags: ,,

Wednesday 23 July 2008

Clear-Stack.ps1

<#
.SYNOPSIS
    Sample of clearing a stack using PowerShell    
.DESCRIPTION
    This script creates a script and shows the impact of clearing the stack
.NOTES
    File Name  : clear-stack.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2
.LINK
    http://pshscripts.blogspot.com/2008/07/clear-stackps1.html
.EXAMPLE
    PSH [C:\foo]: .\clear-stack.ps1
    Initially,
       Count    : 5
       Values:
    jumped
    fox
    brown
    quick
    The
    After Clear,
       Count    : 0
       Values:

#>

##
# Start of script
##

# Create stack
$stack = New-Object system.collections.stack
$Stack.Push( 'The' );
$Stack.Push( 'quick' );
$Stack.Push( 'brown' );
$Stack.Push( 'fox' );
$Stack.Push( 'jumped' );
 
# Display the count and values of the Stack
'Initially,'
'   Count    : {0}' -f $Stack.Count
'   Values:'
$Stack
 
# Clear the Stack
$Stack.Clear()

# Display the count and values of the Stack
'After Clear,'
'   Count    : {0}' -f $Stack.Count
'   Values:'
$Stack

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

Monday 21 July 2008

Get-Stack2.ps1

<#
.SYNOPSIS
    This script shows the use of a stack.
.DESCRIPTION
    This script uses the system.collections.stack
    object.
.NOTES
    File Name  : Get-Stack2.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell Version 2.0
.LINK
    This script posted to:
        http://pshscripts.blogspot.co.uk/2008/07/get-stack2.html
.EXAMPLE
    Psh c:\ .\Get-Stack2.ps1
    Stack as start:
    fox
    brown
    quick
    The

    (Pop)		fox
    Stack value after Pop:
    brown
    quick
    The

    (Pop)		brown
    Stack values after 2nd pop:
    quick
    The

    (Peek)		quick
    Stack values after a peek:
    quick
    The  
#>

# Create and initialise a new stack object
$mystack = new-object system.collections.stack
$myStack.Push( 'The' )
$myStack.Push( 'quick' )
$myStack.Push( 'brown' )
$myStack.Push( 'fox' )

# Display the Stack
'Stack as start:'
$myStack
''# Pop an element from the Stack.
"(Pop)`t`t{0}" -f $myStack.Pop()
'Stack value after Pop:'
$myStack
''

# Pop another element from the Stack
"(Pop)`t`t{0}" -f $myStack.Pop()

# Display the Stack after 2nd pop
'Stack values after 2nd pop:' 
$myStack
''

# Peek at the front
"(Peek)`t`t{0}" -f $myStack.peek()

# Display the Stack after the peek
'Stack values after a peek:' 
$myStack

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

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

Get-DomainFromAOR.ps1

<#
.SYNOPSIS
    Gets the target domain from the AOR and displays result    
.DESCRIPTION
    Defines an AOR, then uses PowerShell to get domain from AOR.
    See http://cacorner.blogspot.com/2008/07/microsoft-office-communicator-auto.html
    for the details behind this mini-script.   
.NOTES
    File Name  : Get-DomainFromAOR.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2 CTP3
.LINK
    See blog posting at:
    http://pshscripts.blogspot.com/2008/07/get-domainfromaorps1.html
.EXAMPLE
    PS C:\Foo> .\get-domainfromaor.ps1
    AOR           : sip:tfl@gktrain.net
    Target Domain : gktrain.net
#>

##
# Start of Script
##

# Determine AOR, then get domain
$AOR = "sip:tfl@gktrain.net"
$domain = ($aor.split("@"))[1]

# Display the results
"AOR           : {0}" -f $aor
"Target Domain : {0}" -f $domain

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

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

Zip-File.ps1

<#
.SYNOPSIS
    This script zips a particular file into an existing archive (zip file).
.DESCRIPTION
    For this script, c:\foo\test.zip must exist
    The script zips c:\test2.txt into c:\foo\test.zip
.NOTES
    File Name  : Zip-File.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2 CTP3
.LINK
    http://pshscripts.blogspot.co.uk/2008/07/zip-fileps1.html
.EXAMPLE
#>

##
# Start of Script
##

# Get shell object and check to see that file exists
$sh = new-object -com shell.application
$fl = get-childitem c:\foo\test.zip -Ea 0
if (!$fl)
 {
   'File does not exist - stopping'
   return
 }

# Get Zip File to zip into
$targetzip = $sh.namespace("C:\Foo\test.zip")   # where the item is to go

# Copy a file into this zip
$targetzip.copyhere("C:\foo\test.txt")

# Check it now exists
ls c:\foo\test.zip
# 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

Unzip-File.ps1

<#
.SYNOPSIS
    This script unzips a particular file from an archive (zip file)
    into a folder.
.DESCRIPTION
    For this script, c:\foo\zip1.zip must exist and contain test.txt
    The script unzips test.txt into c:\foo and tests it's creation. 
    NB: if c:\foo\test.txt alreadyexists, you'll be told and 
    asked (by Windows) to confirm your actions
.NOTES
    File Name  : unzip-file.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    requires   : PowerShell V2
.LINK
    http://pshscripts.blogspot.com/2008/07/unzip-fileps1.html
.EXAMPLE
    PS C:\foo> .\unzip-file.ps1
    Before Copy:
    Target (C:\foo\test.txt)) does not exist
    After copy:
    Target (C:\foo\test.txt) now exists
#>

##
# Start of Script
##
$sh = new-object -com shell.application

# Zip File to unzip from:
$zipfolder    = $sh.namespace("C:\foo\zip1.zip")  # where the .zip is
$item         = $zipfolder.parsename("Test.txt")  # the item in the zip
$targetfolder = $sh.namespace("c:\foo")           # where the item is to go

# first does file exist?
cd c:\foo
$f = ls test.txt -erroraction silentlycontinue
"Before Copy:"
if ($f)  {"Target ($($f.versioninfo.filename)) exists and you will be promoted for an action"} 
if (!$f) {"Target (C:\foo\test.txt)) does not exist"} 

# do the copy of zipfile item to target folder
$targetfolder.copyhere($item)

# check that it exists
"After copy:"
cd c:\foo
$f = ls test.txt -erroraction silentlycontinue
if ($f) {"Target ($($f.versioninfo.filename)) now exists"}
# End of Script