Tinus EngOps Wiki

Logo

OpsaC - Operating as PowerShell code

Links

Home

PowerShell Blog

PowerShell Index

PowerShell Search

Additional Websites

View my GitHub Profile

View my GitHub Gists

View Tinus IT Wiki

View my Photo Website

Write Value to the Registry

tags: PowerShell categories: PowerShell-Registry

Table of Contents

Copy items from a local to a remote computer using a psremote-session.

Write Single-Value to the Registry

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

Volatile registry key

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)

See also

Set-ItemPropertyValue on Microsoft Docs.

[ Top ] [ Blog ]