Friday 30 December 2011

Show-MessageBox.ps1

  1. <#
  2. .SYNOPSIS
  3.     This script displays a message box and then processes it
  4. .DESCRIPTION
  5.     This script firsts creates a wscript.shell object and
  6.     invokes the popup method to display a message. The script
  7.     then processes the response to the geroup (timeout, yes, no).
  8. .NOTES
  9.     File Name : Show-MessageBox.ps1
  10.     Author : Thomas Lee - tfl@psp.co.uk
  11.     Requires : PowerShell Version 2.0
  12. .LINK
  13.     This script posted to:
  14.         http://www.pshscripts.blogspot.com
  15.     MSDN sample posted tot:
  16.         http://msdn.microsoft.com/en-us/library/x83z1d9f%28VS.84%29.aspx
  17. .EXAMPLE
  18.     Left as an exercise to the reader!
  19. #>
  20. # Create the shell object
  21. $WshShell = New-Object -Com Wscript.Shell
  22. # Call the Popup method with a 7 second timeout.
  23. $Btn = $WshShell.Popup("Do you feel alright?", 7, "Question:", 0x4 + 0x20)
  24. # Process the response
  25. switch ($Btn) {
  26. # Yes button pressed.
  27. 6 {"Glad to hear you feel alright."}
  28. # No button pressed.
  29. 7 {"Hope you're feeling better soon."}
  30. # Timed out.
  31. -1 {"Is there anybody out there?"}
  32. }

Monday 12 December 2011

Get-PortAndProtocolFromUrl.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script strips out a port and protocol number from a URL  
  4. .DESCRIPTION 
  5.     This script creates a regular expression reged then uses it to  
  6.     match against the URL to get the protocol and port. This is a 
  7.     re-write of the MSDN sample. 
  8. .NOTES 
  9.     File Name  : Get-PortAndProtocolFromUrl.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.     MSDN sample posted to: 
  16.         http://msdn.microsoft.com/en-us/library/63ew9az0.aspx 
  17. .EXAMPLE 
  18.     PowerShell> .\Get-PortAndProtocolFromUrl.ps1 
  19.     Port    : http 
  20.     Protocol: 8080    
  21. #> 
  22.  
  23. # Set URL 
  24. $url = "http://www.contoso.com:8080/letters/readme.html" 
  25.  
  26. # Create Regex, then match against the URL 
  27. $r = new-object System.Text.RegularExpressions.Regex "^(?<proto>\w+)://[^/]+?:(?<port>\d+)?/" 
  28. $m = $r.Match($url
  29.  
  30. # Print results 
  31. if ($m.Success) { 
  32.    "Port    : {0}" -f $M.groups["proto"].value 
  33.    "Protocol: {0}" -f $M.groups["port"].value 
  34.            

Wednesday 7 December 2011

Confirm-ValidEmailAddress.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script validates email addresses based on 
  4.     MSFT published Regular Expression. This is a  
  5.     re-write with PowerShell of an existing bit of  
  6.     MSDN sample code  
  7. .DESCRIPTION 
  8.     This script first creates a function to validate 
  9.     an email address. It uses a large regex that is 
  10.     documented at the MSDN page noted below. The script 
  11.     then creates an array of email addreses and then 
  12.     validates them against the function and displays 
  13.     the results. 
  14. .NOTES 
  15.     File Name  : Confirm-ValidEmailAddress.ps1 
  16.     Author     : Thomas Lee - tfl@psp.co.uk 
  17.     Requires   : PowerShell Version 2.0 
  18. .LINK 
  19.     This script posted to: 
  20.         http://pshscripts.blogspot.com/2011/12/confirm-validemailaddressps1.html 
  21.     MSDN sample posted to: 
  22.         http://msdn.microsoft.com/en-us/library/01escwtf.aspx  
  23. .EXAMPLE 
  24.     Valid: david.jones@proseware.com 
  25.     Valid: d.j@server1.proseware.com 
  26.     Valid: jones@ms1.proseware.com 
  27.     Invalid: j.@server1.proseware.com 
  28.     Invalid: j@proseware.com9 
  29.     Valid: js#internal@proseware.com 
  30.     Valid: j_9@[129.126.118.1] 
  31.     Invalid: j..s@proseware.com 
  32.     Invalid: js*@proseware.com 
  33.     Invalid: js@proseware..com 
  34.     Invalid: js@proseware.com9 
  35.     Valid: j.s@server1.proseware.com 
  36.     Valid: tfl@psp.co.uk 
  37.     Valid: cuddly.penguin@cookham.net 
  38. #> 
  39.  
  40. Function IsValidEmail   { 
  41. Param ([string] $In
  42. # Returns true if In is in valid e-mail format. 
  43. [system.Text.RegularExpressions.Regex]::IsMatch($In,  
  44.               "^(?("")(""[^""]+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +  
  45.               "(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$");  
  46. } # End of IsValidEmail 
  47.     
  48. [string[]] $emailAddresses = "david.jones@proseware.com", "d.j@server1.proseware.com",  
  49.                             "jones@ms1.proseware.com", "j.@server1.proseware.com",  
  50.                             "j@proseware.com9", "js#internal@proseware.com",  
  51.                             "j_9@[129.126.118.1]", "j..s@proseware.com",  
  52.                             "js*@proseware.com", "js@proseware..com",  
  53.                             "js@proseware.com9", "j.s@server1.proseware.com"
  54.                             "tfl@psp.co.uk", "cuddly.penguin@cookham.net"  
  55.                              
  56. ForEach ($emailAddress in $emailAddresses)    { 
  57.   if (IsValidEmail($emailAddress)) { 
  58.        "Valid: {0}" -f $emailAddress 
  59.        } 
  60.   else
  61.         "Invalid: {0}" -f $emailAddress 
  62.        }     
  63. }                                         

Sunday 20 November 2011

Show-FileInformation.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays information returned from the  
  4.     file version object. 
  5. .DESCRIPTION 
  6.     This script gets, then displays, all the information returned 
  7.     from the System.Diagnostics.Fileinfo of Notepad.exe 
  8. .NOTES 
  9.     File Name  : Show-FileInformation.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://http://pshscripts.blogspot.com 
  15.     MSDN sample posted to: 
  16.          http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo_properties.aspx 
  17. .EXAMPLE 
  18.     Psh> .\Show-FileInformation.ps1 
  19.     File Major Part for C:\Windows\system32\Notepad.exe is: 6             
  20. #> 
  21. # Set filename 
  22. $File = [System.Environment]::SystemDirectory + "\Notepad.exe" 
  23.  
  24. #Get Version information for this file 
  25. $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File
  26.  
  27. # Display all the file information: 
  28. "FileInfo information for {0}" -f $file 
  29. "Comments           : {0}"  -f $myFileVersionInfo.Comments 
  30. "Company Name       : {0}"  -f $myFileVersionInfo.CompanyName 
  31. "FileBuldPart       : {0}"  -f $myFileVersionInfo.FileBuildPart 
  32. "FileDescription    : {0}"  -f $myFileVersionInfo.FileDescription 
  33. "FileMajorPart      : {0}"  -f $myFileVersionInfo.FileMajorPart 
  34. "FileMinorPart      : {0}"  -f $myFileVersionInfo.FileMinorPart 
  35. "FilePrivatePart    : {0}"  -f $myFileVersionInfo.FilePrivatePart 
  36. "FileName           : {0}"  -f $myFileVersionInfo.FileName  
  37. "FileVersion        : {0}"  -f $myFileVersionInfo.FileVersion 
  38. "InternalName       : {0}"  -f $myFileVersionInfo.InternalName 
  39. "IsDebug            : {0}"  -f $myFileVersionInfo.IsDebug 
  40. "IsPatched          : {0}"  -f $myFileVersionInfo.IsPatched 
  41. "IsPreRelease       : {0}"  -f $myFileVersionInfo.IsPreRelease 
  42. "IsPrivateBuild     : {0}"  -f $myFileVersionInfo.IsPrivateBuild 
  43. "IsSpecialBuild     : {0}"  -f $myFileVersionInfo.IsSpecialBuild 
  44. "Language           : {0}"  -f $myFileVersionInfo.Language 
  45. "LegalCopyright     : {0}"  -f $myFileVersionInfo.LegalCopyright 
  46. "LegalTrademarks    : {0}"  -f $myFileVersionInfo.LegalTrademarks 
  47. "OriginalFilename   : {0}"  -f $myFileVersionInfo.OriginalFilename 
  48. "PrivateBuild       : {0}"  -f $myFileVersionInfo.PrivateBuild 
  49. "ProductBuildPart   : {0}"  -f $myFileVersionInfo.ProductBuildPart 
  50. "ProductMajordPart  : {0}"  -f $myFileVersionInfo.ProductMajorPart 
  51. "ProductMinorPart   : {0}"  -f $myFileVersionInfo.ProductMinorPart 
  52. "ProductName        : {0}"  -f $myFileVersionInfo.ProductName 
  53. "ProductPrivatePart : {0}"  -f $myFileVersionInfo.ProductMinorPart 
  54. "ProductVersion     : {0}"  -f $myFileVersionInfo.ProductVersion 
  55. "SpecialBuild       : {0}"  -f $myFileVersionInfo.SpecialBuild 

Sunday 13 November 2011

ShowFileDescription.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script shows the build description of file version object. 
  4. .DESCRIPTION 
  5.     This script is a re-implementation of an MSDN Sample script 
  6.     that uses System.Diagnostics.FileVersionInfo to get 
  7.     the description of the file. 
  8. .NOTES 
  9.     File Name  : Show-FileDescription.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://pshscripts.blogspot.com/2011/11/showfiledescriptionps1.html 
  15.     MSDN sample posted to: 
  16.          http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.filedescription.aspx  
  17. .EXAMPLE 
  18.     Psh> .\Show-FileDescription.ps1 
  19.     File description for C:\Windows\system32\Notepad.exe is: Notepad 
  20. #> 
  21. # Set filename 
  22. $File = [System.Environment]::SystemDirectory + "\Notepad.exe" 
  23.  
  24. #Get Version information for this file 
  25. $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File
  26.  
  27. # Print the Build details name 
  28. "File description for {0} is: {1}"  -f $file,$myFileVersionInfo.FileDescription 

Show-FileBuildPart.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script shows the build number of file version object. 
  4. .DESCRIPTION 
  5.     This script is a re-implementation of an MSDN Sample script 
  6.     that uses System.Diagnostics.FileVersionInfo to get 
  7.     the build identification of the file. 
  8. .NOTES 
  9.     File Name  : Show-FileBuildPart.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://pshscripts.blogspot.com/2011/11/show-filebuildpartps1.html 
  15.     MSDN sample posted to: 
  16.          http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.filebuildpart.aspx   
  17. .EXAMPLE 
  18.     Psh> .\Show-FileBuildPart.ps1 
  19.     File build number for C:\Windows\system32\Notepad.exe is: 6001 
  20. #> 
  21. # Set filename 
  22. $File = [System.Environment]::SystemDirectory + "\Notepad.exe" 
  23.  
  24. #Get Version information for this file 
  25. $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File
  26.  
  27. # Print the Build details name 
  28. "File build number for {0} is: {1}"  -f $file,$myFileVersionInfo.FileBuildPart 

Saturday 12 November 2011

Show-NumberPadding3.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Shows formatting of double values. 
  4. .DESCRIPTION 
  5.     This script, a re-implementation of an MSDN Sample,  
  6.     creates a double value then formats it with 5 leading 
  7.     zeros. 
  8. .NOTES 
  9.     File Name  : Show-NumberPadding3.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://pshscripts.blogspot.com/ 
  15.     MSDN sample posted to: 
  16.         http://msdn.microsoft.com/en-us/library/dd260048.aspx  
  17. .EXAMPLE 
  18.     Psh> Show-NumberPadding3.ps1 
  19.     01053240 
  20.     00103932.52 
  21.     01549230 
  22.  
  23.             01053240 
  24.          00103932.52 
  25.             01549230 
  26.        9034521202.93 
  27.  
  28. #>   
  29.  
  30. $fmt                 = "00000000.##" 
  31. [int]     $intValue  = 1053240 
  32. [decimal] $decValue  = 103932.52 
  33. [float]   $sngValue  = 1549230.10873992 
  34. [double]  $dblValue  = 9034521202.93217412 
  35.  
  36. # Display the numbers using the ToString method 
  37. $intValue.ToString($fmt
  38. $decValue.ToString($fmt
  39. $sngValue.ToString($fmt)            
  40. "" 
  41.  
  42. # Display the numbers using composite formatting 
  43. $formatString = " {0,15:" + $fmt + "}"  # right justified 
  44. $formatString -f $intValue  
  45. $formatString -f $decValue  
  46. $formatString -f $sngValue       
  47. $formatString -f $dblValue 

Show-NumberPadding1.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Shows formatting of leading Zeros 
  4. .DESCRIPTION 
  5.     This script, a re-implementation of an MSDN Sample,  
  6.     creates several numbers of varying type, then 
  7.     displays them using .NET Formatting. The second set 
  8.     of formatting shows the difference between .ToString() 
  9.     and composite format strings to format - to approaches 
  10.     that accomplish the same goal! 
  11. .NOTES 
  12.     File Name  : Show-NumberPadding1.ps1 
  13.     Author     : Thomas Lee - tfl@psp.co.uk 
  14.     Requires   : PowerShell Version 2.0 
  15. .LINK 
  16.     This script posted to: 
  17.         http://pshscripts.blogspot.com/2011/11/show-numberpadding1ps1.html 
  18.     MSDN sample posted to: 
  19.          http://msdn.microsoft.com/en-us/library/dd260048.aspx 
  20. .EXAMPLE 
  21.     Psh> Show-NumberPadding1.ps1 
  22.                   00000254               000000FE 
  23.                   00010342               00002866 
  24.                   01023983               000F9FEF 
  25.                   06985321               006A9669 
  26.        9223372036854775807       7FFFFFFFFFFFFFFF 
  27.       18446744073709551615       FFFFFFFFFFFFFFFF 
  28.      
  29.                   00000254               000000FE 
  30.                   00010342               00002866 
  31.                   01023983               000F9FEF 
  32.        9223372036854775807       7FFFFFFFFFFFFFFF 
  33.       18446744073709551615       FFFFFFFFFFFFFFFF        
  34.  
  35. #> 
  36.  
  37. [byte]   $byteValue   = 254 
  38. [int16]  $shortValue  = 10342 
  39. [int]    $intValue    = 1023983 
  40. [long]   $lngValue    = 6985321 
  41. [long]   $lngValue2   = [System.Int64]::MaxValue 
  42. [UInt64] $ulngValue   = [System.UInt64]::MaxValue 
  43.  
  44. # Display integer values by caling the ToString method. 
  45. "{0,22} {1,22}" -f $byteValue.ToString("D8") , $byteValue.ToString("X8"
  46. "{0,22} {1,22}" -f $shortValue.ToString("D8"), $shortValue.ToString("X8"
  47. "{0,22} {1,22}" -f $intValue.ToString("D8")  , $intValue.ToString("X8"
  48. "{0,22} {1,22}" -f $lngValue.ToString("D8")  , $lngValue.ToString("X8"
  49. "{0,22} {1,22}" -f $lngValue2.ToString("D8") , $lngValue2.ToString("X8"
  50. "{0,22} {1,22}" -f $ulngValue.ToString("D8") , $ulngValue.ToString("X8"
  51. "" 
  52.  
  53. # Display the same integer values by using composite formatting 
  54. "{0,22:D8} {0,22:X8}" -f $byteValue 
  55. "{0,22:D8} {0,22:X8}" -f $shortValue 
  56. "{0,22:D8} {0,22:X8}" -f $intValue 
  57. "{0,22:D8} {0,22:X8}" -f $lngValue2 
  58. "{0,22:D8} {0,22:X8}" -f $ulngValue 

Thursday 10 November 2011

Show-FileVersionCompanyName

  1. <# 
  2. .SYNOPSIS 
  3.     This script shows the company name of a file version object 
  4. .DESCRIPTION 
  5.     This script is a re-implementation of an MSDN Sample script 
  6.     that uses System.Diagnostics.FileVersionInfo to get 
  7.     the company name (if it exists). 
  8. .NOTES 
  9.     File Name  : Show-FileVersionCompanyName.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.     MSDN sample posted to: 
  16.          http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.companyname.aspx    
  17. .EXAMPLE 
  18.     Psh> .\Show-FileVersionCompanyName.ps1 
  19.     Company name: Microsoft Corporation 
  20. #> 
  21. # Set filename 
  22. $File = [System.Environment]::SystemDirectory + "\Notepad.exe" 
  23.  
  24. #Get Version information for this file 
  25. $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File
  26.  
  27. # Print the Company name 
  28. "Company name: " + $myFileVersionInfo.CompanyName 

Show-FileVersionComments.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script shows the comments of a file version object 
  4. .DESCRIPTION 
  5.     This script is a re-implementation of an MSDN Sample script 
  6.     that uses System.Diagnostics.FileVersionInfo to get 
  7.     the company name (if it exists). 
  8. .NOTES 
  9.     File Name  : Show-FileVersionComments.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.     MSDN sample posted to: 
  16.          http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.comments.aspx    
  17. .EXAMPLE 
  18.     Psh> .\Show-FileVersionComments.ps1 
  19.     Comments : Microsoft Corporation 
  20. #> 
  21. # Set filename 
  22. $File = [System.Environment]::SystemDirectory + "\Notepad.exe" 
  23.  
  24. #Get Version information for this file 
  25. $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File
  26.  
  27. # Print the Comments field 
  28. "Commments : " + $myFileVersionInfo.CompanyName 

Wednesday 9 November 2011

Open-WordDocument.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script opens a word document using PowerShell 
  4. .DESCRIPTION 
  5.     This script re-implments a simple MSDN script to open 
  6.     a word document using VBA. IT Pros using PowerShell 
  7.     might also make use of to create rich documents as 
  8.     output from a script. 
  9. .NOTES 
  10.     File Name  : Open-WordDocument.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN sample posted to: 
  17.         http://msdn.microsoft.com/en-us/library/bb148369%28office.12%29.aspx 
  18. .EXAMPLE 
  19.     Running this sample opens the file c:\foo\doc1.docx in Word. The  
  20.     COM object referring to the document is returned for later use. 
  21.      
  22. #> 
  23.  
  24. # Create Word Object 
  25. $wrd = new-object -com word.application 
  26.  
  27. # Make Word Visible 
  28. $wrd.visible = $true 
  29.  
  30. # Open a document  
  31. $doc = $wrd.documents.open("C:\foo\doc1.docx"

Tuesday 8 November 2011

Show-TimeSpan.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script, a re-implementation of an MSDN sample, shows 
  4.     details of a timespan object 
  5. .DESCRIPTION 
  6.     This script re-implements a simple MSDN script that creates a  
  7.     timespan object and displays its properties. 
  8. .NOTES 
  9.     File Name  : Show-TimeSpan.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.     MSDN sample posted to: 
  16.         http://msdn.microsoft.com/en-us/library/system.timespan.aspx 
  17. .EXAMPLE 
  18.      Psh> .\Show-TimeSpan.ps1 
  19.     8/18/2010 1:30:30 PM - 1/1/2010 8:00:15 AM = 229.05:30:15 
  20.        Value of Days Component:                             229 
  21.        Total Number of Days:                   229.229340277778 
  22.        Value of Hours Component:                              5 
  23.        Total Number of Hours:                  5501.50416666667 
  24.        Value of Minutes Component:                           30 
  25.        Total Number of Minutes:                       330090.25 
  26.        Value of Seconds Component:                           15 
  27.        Total Number of Seconds:                      19,805,415 
  28.        Value of Milliseconds Component:                       0 
  29.        Total Number of Milliseconds:             19,805,415,000 
  30.        Ticks:                               198,054,150,000,000 
  31. #> 
  32.  
  33. # Define two dates 
  34. $date1 = new-object system.datetime 2010, 1, 1, 8, 0, 15 
  35. $date2 = new-object system.datetime 2010, 8, 18, 13, 30, 30 
  36.  
  37. # Create a time Interval 
  38. $interval = New-Timespan -start $date1 -end $date2 
  39.  
  40. #Display the interval 
  41. "{0} - {1} = {2}" -f $date2, $date1, $interval.ToString() 
  42.  
  43. # Display individual properties of the resulting TimeSpan object. 
  44. "   {0,-35} {1,20}" -f "Value of Days Component:", $interval.Days 
  45. "   {0,-35} {1,20}" -f "Total Number of Days:", $interval.TotalDays 
  46. "   {0,-35} {1,20}" -f "Value of Hours Component:", $interval.Hours 
  47. "   {0,-35} {1,20}" -f "Total Number of Hours:", $interval.TotalHours 
  48. "   {0,-35} {1,20}" -f "Value of Minutes Component:", $interval.Minutes 
  49. "   {0,-35} {1,20}" -f "Total Number of Minutes:", $interval.TotalMinutes 
  50. "   {0,-35} {1,20:N0}" -f "Value of Seconds Component:", $interval.Seconds 
  51. "   {0,-35} {1,20:N0}" -f "Total Number of Seconds:", $interval.TotalSeconds 
  52. "   {0,-35} {1,20:N0}" -f "Value of Milliseconds Component:", $interval.Milliseconds 
  53. "   {0,-35} {1,20:N0}" -f "Total Number of Milliseconds:", $interval.TotalMilliseconds 
  54. "   {0,-35} {1,20:N0}" -f "Ticks:", $interval.Ticks 

Show-UnicodeCharacters.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script, a re-implementation of an MSDN sample, shows the  
  4.     Unicode details of a unicode character, using PowerShell. 
  5. .DESCRIPTION 
  6.     This script re-implements a simple MSDN script that takes a Unicode Character 
  7.     and uses CharUnicodeInfo class to get details of that character, which are then 
  8.     displayed. 
  9. .NOTES 
  10.     File Name  : Show-UnicodeCharacters.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://pshscripts.blogspot.com/2011/11/show-unicodecharactersps1.html 
  16.     MSDN sample posted to: 
  17.         http://msdn.microsoft.com/en-us/library/system.globalization.charunicodeinfo.aspx
  18. .EXAMPLE 
  19.      Psh > .\show-unicodecharacters.ps1 
  20.                                             c    Num    Dig   Dec    UnicodeCategory 
  21.     U+0061 LATIN SMALL LETTER A             a    -1      -1    -1    LowercaseLetter 
  22.     U+0393 GREEK CAPITAL LETTER GAMMA       Γ    -1      -1    -1    UppercaseLetter 
  23.     U+0039 DIGIT NINE                       9    9       9     9     DecimalDigitNumber 
  24.     U+00B2 SUPERSCRIPT TWO                  ²    2       2     -1    OtherNumber 
  25.     U+00BC VULGAR FRACTION ONE QUARTER      ¼    0.25    -1    -1    OtherNumber 
  26.     U+0BEF TAMIL DIGIT NINE                 ௯    9      9     9     DecimalDigitNumber 
  27.     U+0BF0 TAMIL NUMBER TEN                 ௰    10     -1    -1     OtherNumber 
  28.     U+0F33 TIBETAN DIGIT HALF ZERO          ༳    -0.5    -1    -1     OtherNumber 
  29.     U+2788 CIRCLED SANS-SERIF DIGIT NINE    ➈    9       9    -1     OtherNumber 
  30. #> 
  31.  
  32. # Helper Function 
  33. Function PrintProperties { 
  34.   param ($char
  35.   $fmtstring = " {0,-5}  {1,-8}  {2,-9}  {3,-9}  {4,-9}" 
  36.   $a = $char 
  37.   $b = [System.Globalization.CharUnicodeInfo]::GetNumericValue( $char
  38.   $c = [System.Globalization.CharUnicodeInfo]::GetDigitValue( $char )  
  39.   $d = [System.Globalization.CharUnicodeInfo]::GetDecimalDigitValue( $char
  40.   $e = [System.Globalization.CharUnicodeInfo]::GetUnicodeCategory( $char
  41.   $fmtstring -f $a, $b, $c, $d, $e 
  42. }  
  43. "                                        c      Num      Dig        Dec         UnicodeCategory"  
  44. "U+0061 LATIN SMALL LETTER A            "  + (PrintProperties  "a"
  45. "U+0393 GREEK CAPITAL LETTER GAMMA      "  + (PrintProperties  ([Char] 0x0393) ) 
  46. "U+0039 DIGIT NINE                      "  + (PrintProperties  "9"
  47.  
  48. "U+00B2 SUPERSCRIPT TWO                 "  + (PrintProperties  $([Char] 0x00B2) ) 
  49.  
  50. "U+00BC VULGAR FRACTION ONE QUARTER     "  + (PrintProperties  $([Char] 0x00BC) ) 
  51. "U+0BEF TAMIL DIGIT NINE                "  + (PrintProperties  $([Char] 0x0BEF) ) 
  52. "U+0BF0 TAMIL NUMBER TEN                "  + (PrintProperties  $([Char] 0x0BF0) ) 
  53. "U+0F33 TIBETAN DIGIT HALF ZERO         "  + (PrintProperties  $([Char] 0x0F33) ) 
  54. "U+2788 CIRCLED SANS-SERIF DIGIT NINE   "  + (PrintProperties  $([Char] 0x2788) ) 

Tuesday 1 November 2011

Show-DateTimeFormatInfo.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script re-implements and MSDN sample that shows 
  4.     the different Date/Time Formatting characters and 
  5.     how they are used in formatting date/time objects. 
  6. .DESCRIPTION 
  7.     This script Creates a date/time object, then shows formatting 
  8.     using the key Date/Time Format strings. 
  9. .NOTES 
  10.     File Name  : Show-DateTimeFormatInfo.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN sample posted to: 
  17.         http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx 
  18. .EXAMPLE 
  19.     Left as an exercise for the reader. 
  20.      
  21. #> 
  22.  
  23.  
  24. # Create and initialise a DateTimeFormatInfo associated with the en-US culture. 
  25.  $MyDTFI = (new-object System.Globalization.CultureInfo "en-US", $false ).DateTimeFormat 
  26.  
  27. # Create a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3) 
  28. # The Gregorian calendar is the default calendar for the en-US culture 
  29. $MyDT = new-object System.DateTime  2002, 1, 3  
  30.  
  31. # Display the format pattern associated with each format character 
  32. "FORMAT  en-US EXAMPLE" 
  33. "CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY"  
  34. "  d     {0}"          -f $MyDT.ToString("d", $MyDTFI)  
  35. "        {0}    {1}`n" -f $MyDTFI.ShortDatePattern, "(ShortDatePattern)" 
  36. "  D     {0}"          -f $MyDT.ToString("D", $MyDTFI
  37. "        {0}    {1}`n" -f $MyDTFI.LongDatePattern, "(LongDatePattern)" 
  38. "  f     {0}`n"        -f $MyDT.ToString("f", $MyDTFI
  39. "  F     {0}"          -f $MyDT.ToString("F", $MyDTFI
  40. "        {0}    {1}`n" -f $MyDTFI.FullDateTimePattern, "(FullDateTimePattern)" 
  41. "  g     {0}`n"        -f $MyDT.ToString("g", $MyDTFI
  42. "  G     {0}`n"        -f $MyDT.ToString("G", $MyDTFI
  43. "  m     {0}"          -f $MyDT.ToString("m", $MyDTFI
  44. "        {0}    {1}`n" -f $MyDTFI.MonthDayPattern, "(MonthDayPattern)" 
  45. "  M     {0}"          -f $MyDT.ToString("M", $MyDTFI)  
  46. "        {0}    {1}`n" -f $MyDTFI.MonthDayPattern, "(MonthDayPattern)" 
  47. "  o     {0}`n"        -f $MyDT.ToString("o", $MyDTFI
  48. "  r     {0}"          -f $MyDT.ToString("r", $MyDTFI)  
  49. "        {0}    {1}`n" -f $MyDTFI.RFC1123Pattern, "(RFC1123Pattern)" 
  50. "  R     {0}"          -f $MyDT.ToString("R", $MyDTFI)  
  51. "        {0}    {1}`n" -f $MyDTFI.RFC1123Pattern, "(RFC1123Pattern)" 
  52. "  s     {0}"          -f $MyDT.ToString("s", $MyDTFI)  
  53. "        {0}    {1}`n" -f $MyDTFI.SortableDateTimePattern, "(SortableDateTimePattern)" 
  54. "  t     {0}"          -f $MyDT.ToString("t", $MyDTFI)  
  55. "        {0}    {1}`n" -f $MyDTFI.ShortTimePattern, "(ShortTimePattern)" 
  56. "  T     {0}"          -f $MyDT.ToString("T", $MyDTFI)  
  57. "        {0}    {1}`n" -f $MyDTFI.LongTimePattern, "(LongTimePattern)" 
  58. "  u     {0}"          -f $MyDT.ToString("u", $MyDTFI)  
  59. "        {0}    {1}`n" -f $MyDTFI.UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)" 
  60. "  U     {0}`n"        -f $MyDT.ToString("U", $MyDTFI
  61. "  y     {0}"          -f $MyDT.ToString("y", $MyDTFI)  
  62. "        {0}    {1}`n" -f $MyDTFI.YearMonthPattern, "(YearMonthPattern)" 
  63. "  Y     {0}"          -f $MyDT.ToString("Y", $MyDTFI)  
  64. "        {0}    {1}`n" -f $MyDTFI.YearMonthPattern, "(YearMonthPattern)"