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

Sunday, 11 September 2011

Set-WmiRegistryBinaryValue

  1. <# 
  2. .SYNOPSIS 
  3.     This script sets a registry binary value 
  4.     using WMI. 
  5. .DESCRIPTION 
  6.     This script uses WMI to set a binary  
  7.     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]>New-WmiRegistryBinaryValue.ps1 
  20.     Value created 
  21. #> 
  22.  
  23. # Define Constants 
  24. $HKEY_Local_Machine =2147483650  
  25. $computer ='.' 
  26.  
  27. # Get Class to call static methods on 
  28. $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv" 
  29.  
  30. # Define key to create 
  31. $ValueName = "Example Binary Value" 
  32. $Values     = @(0x54, 0x46, 0x4C) 
  33. $Key       = "SOFTWARE\NewKey" 
  34.  
  35. # Create Value entry 
  36. $results   = $reg.SetBinaryValue($HKEY_LOCAL_MACHINE, $Key, $ValueName, $Values) 
  37. If ($results.Returnvalue -eq 0) {"Value Set"}  

Saturday, 10 September 2011

New-WmiRegistryValue.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a new registry Value using WMI. 
  4. .DESCRIPTION 
  5.     This script uses WMI to get create a new registry Value. 
  6.     This is a re-write of a VB Sample Script 
  7. .NOTES 
  8.     File Name  : New-RegistryKey.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 to: 
  15.         http://msdn.microsoft.com/en-us/library/aa394600%28VS.85%29.aspx 
  16. .EXAMPLE 
  17.     Psh[C:\foo]>New-WmiRegistryValue.ps1 
  18.     Value created 
  19. #> 
  20.  
  21. # Define Constants 
  22. $HKEY_Local_Machine =2147483650  
  23. $computer ='.' 
  24.  
  25. # Get Class to call static methods on 
  26. $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv" 
  27.  
  28. # Define key to create 
  29. $ValueName = "Example_Expanded_String_Value" 
  30. $Value     = "%PATHEXT%" 
  31. $Key       = "SOFTWARE\NewKey" 
  32.  
  33. # Create Value entry 
  34. $results   = $reg.SetExpandedStringValue($HKEY_LOCAL_MACHINE, $Key, $ValueName, $Value) 
  35. If ($results.Returnvalue -eq 0) {"Value created"}  

Set-WmiMultiStringValue.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script sets a registry binary value 
  4.     using WMI. 
  5. .DESCRIPTION 
  6.     This script uses WMI to set a multistring 
  7.     registry Value. 
  8.     This is a re-write of a VB Sample Script. 
  9. .NOTES 
  10.     File Name  : Set-WmiRegistryMultiStringValue.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]>Set-MultiStringValue.ps1 
  20.     Value created 
  21. #> 
  22.  
  23. # Define Constants 
  24. $HKEY_Local_Machine =2147483650  
  25. $computer ='.' 
  26.  
  27. # Get Class to call static methods on 
  28. $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv" 
  29.  
  30. # Define key to create 
  31. $Key       = "SOFTWARE\NewKey" 
  32. $ValueName = "Example MultiString Value" 
  33. $Values     = @("Thomas", "Susan", "Rebecca") 
  34. $Key       = "SOFTWARE\NewKey" 
  35.  
  36. # Create Value entry 
  37. $results   = $reg.SetMultiStringValue($HKEY_LOCAL_MACHINE, $Key, $ValueName, $Values) 
  38. If ($results.Returnvalue -eq 0) {"Value Set"}  

Friday, 9 September 2011

New-WmiRegistryKey.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a new registry key using WMI. 
  4. .DESCRIPTION 
  5.     This script uses WMI to get create a new registry key. 
  6.     This is a re-write of a VB Sample Script 
  7. .NOTES 
  8.     File Name  : New-RegistryKey.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 to: 
  15.         http://msdn.microsoft.com/en-us/library/aa394600%28VS.85%29.aspx 
  16. .EXAMPLE 
  17.     Psh[C:\foo]>New-WMIRegistryKey.ps1 
  18.     Key created 
  19. #> 
  20.  
  21. # Define Constants 
  22. $HKEY_Local_Machine =2147483650  
  23. $computer ='.' 
  24.  
  25. # Get Class to call static methods on 
  26. $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv" 
  27.  
  28. # Define key to create 
  29. $Key     = "SOFTWARE\NewKey" 
  30.  
  31. # Create key and display reslts 
  32. $results   = $reg.CreateKey($HKEY_LOCAL_MACHINE, $Key) 
  33. If ($results.Returnvalue -eq 0) {"Key created"}  

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