PoshCode Logo PowerShell Code Repository

Set-RemoteRegistryKeyPro by Lee Holmes 17 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2222"></script>download | new post

From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes

  1. ##############################################################################
  2. ##
  3. ## Set-RemoteRegistryKeyProperty
  4. ##
  5. ## From Windows PowerShell Cookbook (O'Reilly)
  6. ## by Lee Holmes (http://www.leeholmes.com/guide)
  7. ##
  8. ##############################################################################
  9.  
  10. <#
  11.  
  12. .SYNOPSIS
  13.  
  14. Set the value of a remote registry key property
  15.  
  16. .EXAMPLE
  17.  
  18. PS >$registryPath =
  19.     "HKLM:\software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell"
  20. PS >Set-RemoteRegistryKeyProperty LEE-DESK $registryPath `
  21.       "ExecutionPolicy" "RemoteSigned"
  22.  
  23. #>
  24.  
  25. param(
  26.     ## The computer to connect to
  27.     [Parameter(Mandatory = $true)]
  28.     $ComputerName,
  29.  
  30.     ## The registry path to modify
  31.     [Parameter(Mandatory = $true)]
  32.     $Path,
  33.  
  34.     ## The property to modify
  35.     [Parameter(Mandatory = $true)]
  36.     $PropertyName,
  37.  
  38.     ## The value to set on the property
  39.     [Parameter(Mandatory = $true)]
  40.     $PropertyValue
  41. )
  42.  
  43. Set-StrictMode -Version Latest
  44.  
  45. ## Validate and extract out the registry key
  46. if($path -match "^HKLM:\\(.*)")
  47. {
  48.     $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(
  49.         "LocalMachine", $computername)
  50. }
  51. elseif($path -match "^HKCU:\\(.*)")
  52. {
  53.     $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(
  54.         "CurrentUser", $computername)
  55. }
  56. else
  57. {
  58.     Write-Error ("Please specify a fully-qualified registry path " +
  59.         "(i.e.: HKLM:\Software) of the registry key to open.")
  60.     return
  61. }
  62.  
  63. ## Open the key and set its value
  64. $key = $baseKey.OpenSubKey($matches[1], $true)
  65. $key.SetValue($propertyName, $propertyValue)
  66.  
  67. ## Close the key and base keys
  68. $key.Close()
  69. $baseKey.Close()

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me