OpsaC - Operating as PowerShell code
published: January 24, 2020 author: Tinu tags: PowerShell categories: PowerShell-Registry
Copy items from a local to a remote computer using a psremote-session.
function Set-RegistryValue {
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[Object]$args
)
$ret = $null
if(-not(Test-Path "$($args.Hive):\$($args.Path)")){
New-Item -Path "$($args.Hive):\$($args.Path)" -Force
}
Try{
$ret = Set-ItemProperty -Path "$($args.Hive):\$($args.Path)" -Name $($args.Property) -value $($args.Value) -PassThru
}
catch{
$ret = $null
}
return $ret
}
$params = @{
Hive = 'HKLM'
Path = 'SOFTWARE\Company\Settings'
Property = 'CompanyGroupId'
Value = '123456'
}
Set-RegistryValue -args $params
If you use the New-Item-Cmdlet, the subkey is created as stable key. Sometimes you need a subkey that will be deleted after a reboot, you can create a volatile subkey.
$HKLM =[Microsoft.Win32.RegistryKey]::OpenBaseKey('LocalMachine','default')
$SubKey = $HKLM.OpenSubKey('SOFTWARE\Company\Settings', $true)
$SubKey.CreateSubKey('Volatile', $true , [Microsoft.Win32.RegistryOptions]::Volatile)
$SubKey.CreateSubKey('Non-Volatile', $true , [Microsoft.Win32.RegistryOptions]::None)
Set-ItemPropertyValue on Microsoft Docs.