Monday, 31 October 2011

Show-CurrencyFormatting.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script re-implements an MSDN Sample showing the  
  4.     use of the NumberFormatInfo class to nicely format things 
  5.     in this case, currency. 
  6. .DESCRIPTION 
  7.     This script iterates through the Windows cultures and 
  8.     displays those whose 2-letter ISO code is 'en' and  
  9.     displays how Windows formats currency in that culture.  
  10. .NOTES 
  11.     File Name  : Show-CurrencyFormatting.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 2.0 
  14. .LINK 
  15.     This script posted to: 
  16.         http://www.pshscripts.blogspot.com 
  17.     MSDN sample posted to: 
  18.         http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.aspx 
  19. .EXAMPLE 
  20.     Psh > .\Show-CurrencyFormatting.ps1 
  21.     The currency symbol for 'English (United States)' is '$' 
  22.     The currency symbol for 'English (United Kingdom)' is '£' 
  23.     The currency symbol for 'English (Australia)' is '$' 
  24.     The currency symbol for 'English (Canada)' is '$' 
  25.     The currency symbol for 'English (New Zealand)' is '$' 
  26.     The currency symbol for 'English (Ireland)' is '€' 
  27.     The currency symbol for 'English (South Africa)' is 'R' 
  28.     The currency symbol for 'English (Jamaica)' is 'J$' 
  29.     The currency symbol for 'English (Caribbean)' is '$' 
  30.     The currency symbol for 'English (Belize)' is 'BZ$' 
  31.     The currency symbol for 'English (Trinidad and Tobago)' is 'TT$' 
  32.     The currency symbol for 'English (Zimbabwe)' is 'Z$' 
  33.     The currency symbol for 'English (Republic of the Philippines)' is 'Php' 
  34.     The currency symbol for 'English (Singapore)' is '$' 
  35.     The currency symbol for 'English (Malaysia)' is 'RM' 
  36.     The currency symbol for 'English (India)' is 'Rs.' 
  37.  
  38. #> 
  39.  
  40. #   Loop through all the specific cultures known to the CLR. 
  41. foreach ($ci in [System.Globalization.CultureInfo]::GetCultures([System.Globalization.CultureTypes]::SpecificCultures))  
  42.         { 
  43.             # Only show the currency symbols for cultures that speak English. 
  44.             if ($ci.TwoLetterISOLanguageName -eq "en") { 
  45.             # Display the culture name and currency symbol. 
  46.                $nfi = $ci.NumberFormat 
  47.                "The currency symbol for '{0}' is '{1}'" -f $ci.DisplayName, $nfi.CurrencySymbol 
  48.             } 
  49.         } 

Show-CalendarAlgorithm.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script re-implements an MSDN Sample showing the  
  4.     CalendarAlgorithmType enumeration.  
  5. .DESCRIPTION 
  6.     This script creates there calendars and displays  
  7.     algorithm type, 
  8. .NOTES 
  9.     File Name  : Show-CalendarAlgorithm.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.globalization.calendaralgorithmtype.aspx 
  17. .EXAMPLE 
  18.      
  19. #> 
  20.  
  21. # Helper function 
  22. Function Display{ 
  23. Param ([System.Globalization.Calendar] $c) 
  24. $name = $c.ToString().PadRight(50, '.') 
  25. "{0} {1}" -f $name, $c.AlgorithmType 
  26. } 
  27.  
  28. ## Start of script 
  29.  
  30. # Create three new calendars 
  31. $grCal = new-object System.Globalization.GregorianCalendar 
  32. $hiCal = new-object System.Globalization.HijriCalendar 
  33. $jaCal = new-object System.Globalization.JapaneseLunisolarCalendar 
  34.  
  35. # Display them 
  36. Display($grCal); 
  37. Display($hiCal); 
  38. Display($jaCal); 
  39.  
  40. [enum]::GetNames([System.Globalization.CalendarAlgorithmType]) 

Show-CurrencyGroupSize

  1. <# 
  2. .SYNOPSIS 
  3.     This script reimplements a code sample from MSDN in PowerShell. 
  4.     This sample formats and display currency using standard and 
  5.     different currency groupings.  
  6. .DESCRIPTION 
  7.     This script displays a currency using standard, then two 
  8.     custom CurrencyGroupSizes. 
  9. .NOTES 
  10.     File Name  : Show-CurrencyGroupSize.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 tot: 
  17.         http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencygroupsizes.aspx  
  18. .EXAMPLE 
  19.     PSH:> .\Show-CurrencyGroupSize.ps1 
  20.     Default numeric format string "C" 
  21.     $123,456,789,012,345.00 
  22.  
  23.     Display with array = 2,3,4 
  24.     $12,3456,7890,123,45.00 
  25.  
  26.     Display with array = 2,3,0 
  27.     $1234567890,123,45.00 
  28. #> 
  29.  
  30. #     Get a NumberFormatInfo associated with the en-US culture. 
  31. $fi = new-object System.Globalization.CultureInfo "en-US", false 
  32. $nfi = $fi.NumberFormat 
  33.  
  34. #     Display a value with the default separator (".") 
  35. "Default numeric format string `"C`"" 
  36. [Int64] $myInt = 123456789012345 
  37. $myInt.ToString( "C", $nfi ) 
  38.  
  39. #    Display the same value with different groupings. 
  40. [int[]] $mySizes1 = (2,3,4) 
  41. $mySizes = 2,3,0 
  42.  
  43. "";"Display with array = 2,3,4" 
  44. $nfi.CurrencyGroupSizes = $mySizes1 
  45. $myInt.ToString( "C", $nfi ) 
  46.  
  47. "";"Display with array = 2,3,0" 
  48. $nfi.CurrencyGroupSizes = $mySizes2 
  49. $myInt.ToString( "C", $nfi ) 

Show-StandardNFS-1.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script shows the first two samples on this MSDN Page, 
  4.     convered to PowerShell  
  5. .DESCRIPTION 
  6.     This script displays two lines of text using PowerShell 
  7.     and .NET formatting information 
  8. .NOTES 
  9.     File Name  : Show-StandardNFS-1.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/dwhawy9k.aspx 
  17. .EXAMPLE 
  18.     [Cookham8:C:\foo]> .\Show-StandardNFS.ps1 
  19.     $123.46 
  20.     Your account balance is $123.46. 
  21. #> 
  22.  
  23. # Show formatting a number as currency 
  24. $value = 123.456 
  25. $value.ToString("C2") 
  26.  
  27. # Show the -f operator with currency formatting 
  28. $value = 123.456 
  29. "Your account balance is {0:C2}." -f $value 

Tuesday, 11 October 2011

Get-OSInstallDate.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets/displays the Sysgtem Uptime after 
  4.     converting it from WEBM time/date format  
  5. .DESCRIPTION 
  6.     This script creates an instance of a SWbemDateTime object,  
  7.     gets the OS install date and converts the date to a more 
  8.     useful format. 
  9. .NOTES 
  10.     File Name  : Get-OSInstallDate.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/aa393687%28VS.85%29.aspx 
  18. .EXAMPLE 
  19.     Psh [C:\foo]> .\Get-OsInstallDate.ps1 
  20.     This OS was installed in the year 2008 
  21.     Full installation date (VT_DATE format) is 4/9/2008 8:17:23 PM 
  22.     Full installation date (FILETIME format) is 128522458430000000 
  23.  
  24. #> 
  25.  
  26. # Create swbemdatetime object 
  27. $datetime = New-Object -ComObject WbemScripting.SWbemDateTime 
  28.  
  29. #  Get OS installation time and assign to datetime object 
  30. $os = Get-WmiObject -Class Win32_OperatingSystem 
  31. $dateTime.Value = $os.InstallDate 
  32.  
  33. # Now display the time 
  34. "This OS was installed in the year {0}"           -f $dateTime.Year 
  35. "Full installation date (VT_DATE format) is {0}"  -f $dateTime.GetVarDate() 
  36. "Full installation date (FILETIME format) is {0}" -f $dateTime.GetFileTime()  
  37. ## 

Friday, 7 October 2011

Register-Event1.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script registers for a WMI event 
  4. .DESCRIPTION 
  5.     This script used PowerShell to register for then display 
  6.     an event. This script is a re-write on an MSDN Sample. 
  7. .NOTES 
  8.     File Name  : Register-Event1.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://www.pshscripts.blogspot.com 
  14.     MSDN sample posted tot: 
  15.         http://msdn.microsoft.com/en-us/library/aa393013%28VS.85%29.aspx 
  16. .EXAMPLE 
  17.     [PSH] C:\FOO> Register-Event1.ps1 
  18.     Waiting for events 
  19.     Log Event Occured 
  20.     EVENT MESSAGE 
  21.     A logon was attempted using explicit credentials. 
  22.  
  23.     Subject: 
  24.     Security ID:        S-1-5-21-2824006062-479960714-4144511058-1105 
  25.     Account Name:       tfl 
  26.     ...  -> Reminder snipped for brevity 
  27.     
  28. #> 
  29.  
  30. # Define event Query 
  31. $query = "SELECT * FROM __InstanceCreationEvent  
  32.           WHERE TargetInstance ISA 'Win32_NTLogEvent' " 
  33.  
  34. # Register for event - also specify an action that 
  35. # displays the log event when the event fires. 
  36. Register-WmiEvent -Source Demo1 -Query $query -Action { 
  37.                 Write-Host "Log Event occured" 
  38.                 $global:myevent = $event 
  39.                 Write-Host "EVENT MESSAGE" 
  40.                 Write-Host $event.SourceEventArgs.NewEvent.TargetInstance.Message} 
  41. # So wait 
  42. "Waiting for events" 
Technorati Tags: ,,,

Friday, 30 September 2011

Get-FolderObjects.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the WMI objects associated with a folder 
  4. .DESCRIPTION 
  5.     This script uses the Associators Of query 
  6. .NOTES 
  7.     File Name  : get-folderobjects.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13. .EXAMPLE 
  14.     Get-FolderObjects "C:\Foo" 
  15.         what's in C:\foo 
  16.     0 Volume objects 
  17.     0 Logical Disk objects 
  18.     10 Directory objects 
  19.     171 File objects 
  20.     0 Pagefile objects 
  21.     1 Security Settings 
  22.     0 Share objects 
  23.     0 other objects 
  24. .EXAMPLE 
  25.     Get-FolderObjects "C:\" 
  26.       what's in C:\ 
  27.     1 Volume objects 
  28.     1 Logical Disk objects 
  29.     18 Directory objects 
  30.     8 File objects 
  31.     1 Pagefile objects 
  32.     1 Security Settings 
  33.     1 Share objects 
  34.     0 other objects   
  35. #> 
  36. # Function to get folder WMI object associations 
  37. # Define function 
  38. Function Get-FolderObjects { 
  39. Param ( 
  40. $Folder  =   "c:\" 
  41. ) 
  42.  
  43. # Set query then get associations 
  44. $query  = "ASSOCIATORS OF {Win32_Directory.Name='$folder'}" 
  45. $objs = Gwmi -q $query 
  46. #    
  47. # Now group them 
  48. # Create empty arrays 
  49. $directory = @() 
  50. $datafile  = @() 
  51. $pagefile  = @() 
  52. $volume    = @() 
  53. $filesec   = @() 
  54. $share     = @() 
  55. $Logdisk   = @() 
  56. $unknown   = @() 
  57.  
  58. # Now fill the arrays 
  59. foreach ($obj in $objs) { 
  60.     switch  ($Obj.__class) { 
  61.       "Win32_Directory"   {$directory += $obj; break} 
  62.       "Cim_DataFile"      {$datafile  += $obj; break} 
  63.       "Win32_PageFile"    {$pagefile  += $obj; break} 
  64.       "Win32_Volume"      {$volume    += $obj; break} 
  65.       "Win32_LogicalDisk" {$logdisk   += $obj; break}  
  66.       "Win32_LogicalFileSecuritySetting" {$filesec += $obj; break} 
  67.       "Win32_share"       {$share     += $obj; break} 
  68.       default             {$unknown   += $obj; break} 
  69.     } 
  70. } 
  71.  
  72. # Display the output 
  73. " what's in $folder" 
  74. "{0} Volume objects"        -f $volume.count 
  75. "{0} Logical Disk objects"  -f $logdisk.count 
  76. "{0} Directory objects"     -f $directory.count 
  77. "{0} File objects"          -f $datafile.count 
  78. "{0} Pagefile objects"      -f $pagefile.count 
  79. "{0} Security Settings"     -f $filesec.count 
  80. "{0} Share objects"         -f $share.count 
  81. "{0} other objects"         -f $unknown.count 
  82. } 
  83.  
  84. # Here call function as an example 
  85. Get-FolderObjects "C:\" 
  86. "***" 
  87. Get-FolderObjects "C:\foo" 

Sunday, 25 September 2011

Set-ComputerName.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script renames a computer using WMI. 
  4. .DESCRIPTION 
  5.     This script uses the Rename method from
  6.     the Win32_OperatingSystem WMI class.  
  7.     This is sample 5 from http://msdn.microsoft.com/en-us/library/aa394586 
  8. .NOTES 
  9.     File Name  : Set-ComputerName.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/aa394586%28VS.85%29.aspx 
  17. .EXAMPLE 
  18.     Left as an exercise for the Reader 
  19. #> 
  20. # This script takes two parameters, The computer to  
  21. # rename and then the newname for that computer 
  22.  
  23. param ( 
  24. [$String] $NewName = 'NewName', 
  25. [$string] $Comp = "." 
  26. } 
  27.  
  28. # Get computer object 
  29. $Computer = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $comp 
  30.  
  31. #Rename the Computer 
  32. $Return  = $Computer.Rename($NewName) 
  33.  
  34. if ($return.ReturnValue -eq 0) { 
  35.    "Computer name is now: $NewName" 
  36.    " but you need to reboot first" 
  37. } else { 
  38. "  RenameFailed, return code: {0}" -f $return.ReturnValue 
  39. } 

Tuesday, 13 September 2011

Remove-WmiRegistryKey.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates removes registry key using WMI. 
  4. .DESCRIPTION 
  5.     This script uses WMI to get remove registry key.  
  6.     This script deletes the key and everything below 
  7.     it in the registry - use carefully! 
  8. .NOTES 
  9.     File Name  : Remove-WmiRegistryKey.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/aa394600%28VS.85%29.aspx 
  17. .EXAMPLE 
  18.     Psh[C:\foo]>New-WMIRegistryKey.ps1 
  19.     Key removed 
  20. #> 
  21.  
  22. # Define Constants 
  23. $HKEY_Local_Machine =2147483650  
  24. $computer ='.' 
  25.  
  26. # Get Class to call static methods on 
  27. $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv" 
  28.  
  29. # Define key to create 
  30. $Key     = "SOFTWARE\NewKey" 
  31.  
  32. # Create key and display reslts 
  33. $results   = $reg.DeleteKey($HKEY_LOCAL_MACHINE, $Key) 
  34. If ($results.Returnvalue -eq 0) {"Key Removed"}  

Monday, 12 September 2011

Get-WmiRegistryBinaryValue.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script Gets and displays a registry 
  4.     binary value using WMI. 
  5. .DESCRIPTION 
  6.     This script uses WMI to get, then display 
  7.     a binary registry Value. 
  8.     This is a re-write of a VB Sample Script. 
  9. .NOTES 
  10.     File Name  : Set-WmiRegistryBinaryValue.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/aa394600%28VS.85%29.aspx 
  18. .EXAMPLE 
  19.     Psh[C:\foo]>Get-WmiRegistryBinaryValue.ps1 
  20.     54 
  21.     46 
  22.     4c 
  23. #> 
  24.  
  25. # Define Constants 
  26. $HKEY_Local_Machine =2147483650  
  27. $computer ='.' 
  28.  
  29. # Get Class to call static methods on 
  30. $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv" 
  31.  
  32. # Define key to create 
  33. $ValueName = "Example Binary Value" 
  34. $Values     = @(0x54, 0x46, 0x4C) 
  35. $Key       = "SOFTWARE\NewKey" 
  36.  
  37. # Create Value entry 
  38. $results   = $reg.GetBinaryValue($HKEY_LOCAL_MACHINE, $Key, $ValueName) 
  39. Foreach ($byte in $results.uvalue) {"{0}" -f $byte.tostring("x")} 

W