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. # Get computer object 
  28. $Computer = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $comp 
  29.  
  30. #Rename the Computer 
  31. $Return  = $Computer.Rename($NewName
  32.  
  33. if ($return.ReturnValue -eq 0) { 
  34.    "Computer name is now: $NewName" 
  35.    " but you need to reboot first" 
  36. } else
  37. "  RenameFailed, return code: {0}" -f $return.ReturnValue 

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: ,,,