Thursday, 8 September 2011

Get-WmiRegDword.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets a registry value using WMI. 
  4. .DESCRIPTION 
  5.     This script uses WMI to get then display a resistry value, using  
  6.     the SteRegProv class and the GetDWORDValue static method. 
  7.     This is a re-write of a VB Sample Script 
  8. .NOTES 
  9.     File Name  : Get-WMIRegDword.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]>Get-WmiRegDword.ps1 
  19. Current History Buffer Size: 50 
  20.      
  21. #> 
  22.  
  23. # Define Constants 
  24. $HKEY_CURRENT_USER =2147483649   
  25. $computer ='.' 
  26.  
  27. # Get Class to call static methods on 
  28. $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv" 
  29.  
  30. #defind key/value to get  
  31. $Key     = "Console" 
  32. $Value   = "HistoryBufferSize" 
  33.  
  34. # Get Value of buffer and display 
  35. $results   = $reg.GetDWORDValue($HKEY_CURRENT_USER, $Key, $value
  36. "Current History Buffer Size: {0}" -f $results.uValue 
Technorati Tags: ,,,

Sunday, 14 August 2011

Show-MajorPart.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script shows the Major part of the version number 
  4.     of file version object. 
  5. .DESCRIPTION 
  6.     This script is a re-implementation of an MSDN Sample script 
  7.     that uses System.Diagnostics.FileVersionInfo to get 
  8.     the major part of the version number of the file. 
  9. .NOTES 
  10.     File Name  : Show-MajorPart.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/08/show-majorpartps1.html 
  16.     MSDN sample posted to: 
  17.          http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.filemajorpart.aspx   
  18. .EXAMPLE 
  19.     Psh> .\Show-MajorPart.ps1 
  20.     File Major Part for C:\Windows\system32\Notepad.exe is: 6             
  21. #> 
  22. # Set filename 
  23. $File = [System.Environment]::SystemDirectory + "\Notepad.exe" 
  24.  
  25. #Get Version information for this file 
  26. $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File
  27.  
  28. # Print the Build details name 
  29. "File Major Part for {0} is: {1}"  -f $file,$myFileVersionInfo.FileMajorPart 

Thursday, 14 July 2011

Show-MinorPart.ps1

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

Sunday, 19 June 2011

Get-ParsedInteger.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script parses several strings into integers, where possible.  
  4. .DESCRIPTION 
  5.     This script uses the TryParse method on [Int32] to attempt 
  6.     to parse a string into a number and writes the results. This script 
  7.     is a recoded MSDN sample using PowerShell 
  8. .NOTES 
  9.     File Name  : Get-ParsedInt32.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/f02979c7.aspx 
  17. .EXAMPLE 
  18.     Psh[C:\foo]> .\get-parsedinteger.ps1 
  19. Attempted conversion of '' failed. 
  20. Converted '160519' to 160519. 
  21. Attempted conversion of '9432.0' failed. 
  22. Attempted conversion of '16,667' failed. 
  23. Converted '   -322   ' to -322. 
  24. Converted '+4302' to 4302. 
  25. Attempted conversion of '(100);' failed. 
  26. Attempted conversion of '01FA' failed.     
  27. #> 
  28.  
  29. # Define function to parse a string to integer 
  30. Function TryToParse { 
  31. # Parameter to parse into a number 
  32. Param ([string] $value
  33. # Define $number and try the parse 
  34. [int] $number = 0 
  35. $result = [System.Int32]::TryParse($value, [ref] $number); 
  36. if ($result)  { 
  37.       "Converted '{0}' to {1}." -f $value, $number  
  38.               } 
  39.       else    { 
  40.         if ($value -eq $null) {$value = ""}  
  41.        "Attempted conversion of '{0}' failed." -f $value 
  42.               } 
  43.  
  44. # Now call the function to see if the string will parse 
  45. TryToParse($null
  46. TryToParse("160519"); 
  47. TryToParse("9432.0"); 
  48. TryToParse("16,667"); 
  49. TryToParse("   -322   "); 
  50. TryToParse("+4302"); 
  51. TryToParse("(100);"); 
  52. TryToParse("01FA"); 
Technorati Tags: ,

Tuesday, 29 March 2011

New-Credential.ps1

  1. <# 
  2. .SYNOPSIS 
  3.    A function to create a credential object from script. 
  4. .DESCRIPTION 
  5.    Enables you to create a credential objects from stored details. 
  6. .NOTES 
  7.     File Name  : New-Credential.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://pshscripts.blogspot.com/2011/03/new-credentialps1.html 
  13. .PARAMETER UserId 
  14.    The userid in the form of "domain\user" 
  15. .PARAMETER Password 
  16.    The password for this user 
  17. .EXAMPLE 
  18.    New-Credential contoso\administrator  Pa$$w0rd 
  19. #> 
  20.  
  21. function New-Credential { 
  22. param ( 
  23. [string] $Userid, 
  24. [string] $Pwd 
  25. # Create the credential 
  26. $spwd = ConvertTo-SecureString -AsPlainText $pwd -Force  
  27. $cred = New-Object System.Management.Automation.PSCredential $userid,$spwd 
  28. # Now return it to the caller 
  29. return $cred 
  30.  
  31. # Call the function to demostrate example 
  32. New-Credential "contoso\administrator" "Pa$$w0rd" 
Technorati Tags: ,,

Monday, 14 February 2011

New-Shortcut.ps1

  1. <# 
  2. .SYNOPSIS 
  3.    This script creates a shortcut to Notepad on the Desktop 
  4. .DESCRIPTION 
  5.    This script creates a Wscript.shell item, then creates  
  6.    a shortcut on the desktop to Notepad.exe. 
  7. .NOTES 
  8.     File Name  : New-Shortcut.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINKS 
  12.     This post is a re-implementation of an MSDN Script 
  13.         http://msdn.microsoft.com/en-us/library/0ea7b5xe%28VS.85%29.aspx 
  14.     Posted to Powershell Scripts Blog 
  15.         HTTP://Pshscripts.blogspot.com 
  16. .EXAMPLE 
  17.    Left as an exercise for the reader 
  18. #> 
  19.  
  20. # create wscript object 
  21. $WshShell = New-Object -com Wscript.Shell 
  22.  
  23. #Get Desktop location 
  24. $Desktop  = $WshShell.SpecialFolders.item("Desktop"
  25.  
  26. # create a new shortcut 
  27. $ShellLink                  = $WshShell.CreateShortcut($Desktop + "\Shortcut Script.lnk"
  28. $ShellLink.TargetPath       = $WScript.ScriptFullName 
  29. $ShellLink.WindowStyle      = 1 
  30. $ShellLink.Hotkey           = "CTRL+SHIFT+F" 
  31. $ShellLink.IconLocation     = "notepad.exe, 0" 
  32. $ShellLink.Description      = "Shortcut Script" 
  33. $ShellLink.WorkingDirectory = $Desktop 
  34.  
  35. #Save the link to the desktop 
  36. $ShellLink.Save() 

Tuesday, 4 January 2011

Get-ComputerDomain.ps1

<#
.SYNOPSIS
This script uses WMI to get the name of a computer's domain then displays the name.
.DESCRIPTION
This script is a re-write of an MSDN sample, using PowerShell.
.NOTES
File Name : Get-ComputerDomain.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN sample posted tot:
http://msdn.microsoft.com/en-us/library/aa394586%28VS.85%29.aspx/
.EXAMPLE
PSH [C:\foo]: .\Get-ComputerDomain.ps1
System Name: WIN7
Domain: cookham.net
.EXAMPLE
Psh[Cookham8]> .\Get-ComputerDomain.ps1 cookham2
System Name: COOKHAM2
Domain: cookham.net
.PARAMETER comp
The name of the computer whose domain name to be displayed. Default is localhost.
#>


# Parameter block
param (
$comp = "."
)

# Get WMI Object
$system = Get-WmiObject -class Win32_ComputerSystem -ComputerName $comp

# Display results
"System Name: {0}" -f $System.Name
"Domain: {0}" -f $System.Domain


Friday, 31 December 2010

Count-GDDuplicate.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Finds duplicates in the GD archive – A script
  4.     for New Year’s eve! 
  5. .DESCRIPTION 
  6.  
  7. .NOTES 
  8.     File Name  : Count-GDDuplicate.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 
  11. .LINK 
  12.     http://pshscripts.blogspot.com/2010/12/count-gdduplicateps1.html 
  13. .EXAMPLE 
  14.     Psh[Cookham8]>C:\foo\Count-GdDuplicate.ps1 
  15.     Count-GDDuplicate.ps1 - v 1.0.1 
  16.  
  17.     99 shows have duplicates 
  18.     109 shows are duplicates of others 
  19. #> 
  20.  
  21. ### 
  22. # Start of Script 
  23. ## 
  24.  
  25. # Constants: 
  26. # $GDDiskRoot    - where to find shows 
  27. # $DeadShowBase  - folder at top of gd shows 
  28.  
  29. $GDDiskRoot = "M:" 
  30. $DeadShowBase = $GDDiskRoot + "\gd" 
  31.  
  32. # Announce Ourselves 
  33. "DuplicateCount.ps1 - v 0.0.1" 
  34. "+---------------------------------+" 
  35. "! Find Duplicate Shows :  $DeadShowBase   !" 
  36. "+---------------------------------+" 
  37. "" 
  38. # Get start time 
  39. $starttime = Get-Date 
  40. set-strictmode -off 
  41.  
  42. # Get the Dead shows 
  43.  
  44. $Dir = ls $DeadShowBase | where {$_.psiscontainer} 
  45. $DeadShows = $Dir.count 
  46. if ($DeadSHows -le 0) {"no shows found - check constants"; return
  47.  
  48. # So here look for duplicates. 
  49.  
  50. $shows =@{} 
  51.  
  52. $j = 0 
  53. foreach ($show in $dir){ 
  54. $j++ 
  55. $showdate = $show.name.substring(0,10) 
  56. if ($shows.$showdate) { 
  57.   $shows.$showdate++ 
  58. }   
  59. else
  60.   $shows += @{$showdate=1} 
  61. }    
  62.  
  63.  
  64. $shows = $shows.getenumerator() | Sort name 
  65. $totaldups = 0 
  66. $totaldupshows = 0 
  67. foreach ($show in $shows){ 
  68.    if ($show.value -gt 1) { 
  69.      # "{1} copies of: {0}" -f  $show.name, $show.value 
  70.      $totaldups++    
  71.      $totaldupshows += $show.value - 1 # anything after the 1st is a dup 
  72.    } 
  73.  
  74. # Display Summary 
  75. "{0} shows have duplicates" -f $totaldups 
  76. "{0} shows are duplicates of others" -f $totaldupshows 
Technorati Tags:

Thursday, 23 December 2010

Set-IseThemeVim.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script sets an ISE Theme to similar to the old VIM editor. 
  4. .DESCRIPTION 
  5.     This script sets the key values in $PsIse.Options to values consistent 
  6.     with the VIM editor, beloved by many, particularly on the Powershell 
  7.     product team. This script is based on Davis Mohundro's blog post
  8.     (http://bit.ly/iib5IM), updated for RTM of PowerShell V2.0. See also  
  9. .NOTES 
  10.     File Name  : Set-ISEThemeVIM.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 (ISE only) 
  13. .LINK 
  14.     This script posted to: 
  15.         http://pshscriptsbog.blogspot.com  
  16.         http://bit.ly/iaA2iX
  17. .EXAMPLE 
  18.     This script when run resets colours on key panes, including 
  19.     colourising tokens in the script pane. Try it and see it... 
  20. #> 
  21.  
  22.  
  23. # PowerShell ISE version of the VIM blackboard theme at  
  24. # http://www.vim.org/scripts/script.php?script_id=2280 
  25.  
  26. # Set font name and size 
  27. $psISE.Options.FontName = 'Courier New' 
  28. $psISE.Options.FontSize = 16 
  29.  
  30. # Set colours for output pane 
  31. $psISE.Options.OutputPaneBackgroundColor     = '#FF000000' 
  32. $psISE.Options.OutputPaneTextBackgroundColor = '#FF000000' 
  33. $psISE.Options.OutputPaneForegroundColor     = '#FFFFFFFF' 
  34.  
  35. # Set colours for command pane 
  36. $psISE.Options.CommandPaneBackgroundColor    = '#FF000000' 
  37.  
  38. # Set colours for script pane 
  39.  
  40. $psise.options.ScriptPaneBackgroundColor    ='#FF000000' 
  41.  
  42. # Set colours for tokens in Script Pane 
  43. $psISE.Options.TokenColors['Command'] = '#FFFFFF60' 
  44. $psISE.Options.TokenColors['Unknown'] = '#FFFFFFFF' 
  45. $psISE.Options.TokenColors['Member'] = '#FFFFFFFF' 
  46. $psISE.Options.TokenColors['Position'] = '#FFFFFFFF' 
  47. $psISE.Options.TokenColors['GroupEnd'] = '#FFFFFFFF' 
  48. $psISE.Options.TokenColors['GroupStart'] = '#FFFFFFFF' 
  49. $psISE.Options.TokenColors['LineContinuation'] = '#FFFFFFFF' 
  50. $psISE.Options.TokenColors['NewLine'] = '#FFFFFFFF' 
  51. $psISE.Options.TokenColors['StatementSeparator'] = '#FFFFFFFF' 
  52. $psISE.Options.TokenColors['Comment'] = '#FFAEAEAE' 
  53. $psISE.Options.TokenColors['String'] = '#FF00D42D' 
  54. $psISE.Options.TokenColors['Keyword'] = '#FFFFDE00' 
  55. $psISE.Options.TokenColors['Attribute'] = '#FF84A7C1' 
  56. $psISE.Options.TokenColors['Type'] = '#FF84A7C1' 
  57. $psISE.Options.TokenColors['Variable'] = '#FF00D42D' 
  58. $psISE.Options.TokenColors['CommandParameter'] = '#FFFFDE00' 
  59. $psISE.Options.TokenColors['CommandArgument'] = '#FFFFFFFF' 
  60. $psISE.Options.TokenColors['Number'] = '#FF98FE1E' 
Technorati Tags: ,,,

Set-ISEThemeDefault.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script resets the ISE to default ‘theme’. 
  4. .DESCRIPTION 
  5.     This script sets the key values in $PsIse.Options to their default 
  6.     options in $Psise.Options.DefaultOptions. This script is useful if you 
  7.     are playing with ISE options and don't quite get it right - just run this  
  8.     script to set things back to default.        
  9. .NOTES 
  10.     File Name  : Set-ISEThemeDefault.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 (ISE only) 
  13. .LINK 
  14.     This script posted to: 
  15.         http://bit.ly/gJpu2W  
  16. .EXAMPLE 
  17.     This script when run just resets the theme to the default. To view, run from 
  18.     the ISE after changing options! 
  19. #> 
  20.  
  21. # Set Basic options 
  22. $psise.options.SelectedScriptPaneState = $psise.options.DefaultOptions.SelectedScriptPaneState 
  23. $psise.options.ShowToolBar             = $psise.options.DefaultOptions.ShowToolBar 
  24. $psise.options.FontSize                = $psise.options.DefaultOptions.FontSize 
  25. $psise.options.Fontname                = $psise.options.DefaultOptions.Fontname 
  26.  
  27. # set colouring 
  28. $psise.options.ErrorForegroundColor          = $psise.options.DefaultOptions.ErrorForegroundColor 
  29. $psise.options.ErrorBackgroundColor          = $psise.options.DefaultOptions.ErrorBackgroundColor 
  30. $psise.options.WarningForegroundColor        = $psise.options.DefaultOptions.WarningForegroundColor 
  31. $psise.options.WarningBackgroundColor        = $psise.options.DefaultOptions.WarningBackgroundColor 
  32. $psise.options.VerboseForegroundColor        = $psise.options.DefaultOptions.VerboseForegroundColor 
  33. $psise.options.VerboseBackgroundColor        = $psise.options.DefaultOptions.VerboseBackgroundColor 
  34. $psise.options.DebugBackgroundColor          = $psise.options.DefaultOptions.DebugBackgroundColor 
  35. $psise.options.DebugForegroundColor          = $psise.options.DefaultOptions.DebugForegroundColor 
  36. $psise.options.OutputPaneBackgroundColor     = $psise.options.DefaultOptions.OutputPaneBackgroundColor 
  37. $psise.options.OutputPaneTextBackgroundColor = $psise.options.DefaultOptions.OutputPaneTextBackgroundColor 
  38. $psise.options.OutputPaneForegroundColor     = $psise.options.DefaultOptions.OutPutPaneForegroundColor 
  39. $psise.options.CommandPaneBackgroundColor    = $psise.options.DefaultOptions.CommandPaneBackgroundColor 
  40. $psise.options.ScriptPaneBackgroundColor     = $psise.options.DefaultOptions.ScriptPaneBackgroundColor  
  41. $psise.options.ScriptPaneForegroundColor     = $psise.options.DefaultOptions.ScriptPaneForegroundColor  
  42.  
  43. # More options 
  44. $psise.options.ShowWarningForDuplicateFiles  = $psise.options.DefaultOptions.ShowWarningForDuplicateFiles   
  45. $psise.options.ShowWarningBeforeSavingOnRun  = $psise.options.DefaultOptions.ShowWarningBeforeSavingOnRun 
  46. $psise.options.UseLocalHelp                  = $psise.options.DefaultOpitons.UseLocalHelp 
  47. $psise.options.CommandPaneUp                 = $psise.options.DefaultOptions.CommandPaneUp 
  48.  
  49. # Reset Tokens Colors 
  50. $psISE.Options.TokenColors['Attribute']          = $psISE.Options.DefaultOptions.TokenColors['Attribute'] 
  51. $psISE.Options.TokenColors['Command']            = $psISE.Options.DefaultOptions.TokenColors['Command'] 
  52. $psISE.Options.TokenColors['CommandArgument']    = $psISE.Options.DefaultOPtions.TokenColors['CommandArgument'] 
  53. $psISE.Options.TokenColors['CommandParameter']   = $psISE.Options.DefaultOptions.TokenColors['CommandParameter'] 
  54. $psISE.Options.TokenColors['Comment']            = $psISE.Options.DefaultOptions.TokenColors['Comment'] 
  55. $psISE.Options.TokenColors['GroupEnd']           = $psISE.Options.DefaultOptions.TokenColors['GroupEnd'] 
  56. $psISE.Options.TokenColors['GroupStart']         = $psISE.Options.DefaultOptions.TokenColors['GroupStart'] 
  57. $psISE.Options.TokenColors['Keyword']            = $psISE.Options.DefaultOptions.TokenColors['Keyword'] 
  58. $psISE.Options.TokenColors['LineContinuation']   = $psISE.Options.DefaultOptions.TokenColors['LineContinuation'] 
  59. $psISE.Options.TokenColors['LoopLabel']          = $psISE.Options.DefaultOptions.TokenColors['LoopLabel'] 
  60. $psISE.Options.TokenColors['Member']             = $psISE.Options.DefaultOptions.TokenColors['Member'] 
  61. $psISE.Options.TokenColors['NewLine']            = $psISE.Options.DefaultOptions.TokenColors['NewLine'] 
  62. $psISE.Options.TokenColors['Number']             = $psISE.Options.DefaultOPtions.TokenColors['Number'] 
  63. $psISE.Options.TokenColors['Position']           = $psISE.Options.DefaultOptions.TokenColors['Position'] 
  64. $psISE.Options.TokenColors['StatementSeparator'] = $psISE.Options.DefaultOptions.TokenColors['StatementSeparator'] 
  65. $psISE.Options.TokenColors['String']             = $psISE.Options.DefaultOptions.TokenColors['String'] 
  66. $psISE.Options.TokenColors['Type']               = $psISE.Options.DefaultOptions.TokenColors['Type']  
  67. $psISE.Options.TokenColors['Unknown']            = $psISE.Options.DefaultOptions.TokenColors['Unknown'] 
  68. $psISE.Options.TokenColors['Variable']           = $psISE.Options.DefaultOptions.TokenColors['Variable'] 
  69.  
  70. # Done