functions/Update-RegistryItem.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<#
    .SYNOPSIS
    Sets or creates the value for the specified registry entry.
 
    .DESCRIPTION
    Update-RegistryItem tests if the specified registry entry exists, if it does, then it calls Set-ItemProperty to set the value. If the entry doesn't exist, it calls New-ItemProperty.
 
    The reason for me to create this was to simplify DSC resources.
 
    .PARAMETER Confirm
    Prompts you for confirmation before running the cmdlet.
 
    .PARAMETER WhatIf
    Shows what would happen if the cmdlet runs. The cmdlet is not run.
 
    .EXAMPLE
    Update-RegistryItem -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management' -Name 'FeatureSettingsOverride' -Value 0 -PropertyType 'DWORD'
    Sets or creates the registry value FeatureSettingsOverride at HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management to 0.
#>

function Update-RegistryItem
{
    [CMDLetBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
    Param (
        # Specifies the path of the entry. This parameter identifies the entry to which this cmdlet sets or creates the new property.
        [Parameter(Mandatory = $true)]
        [ValidateNotNullorEmpty()]
        [String]
        $Path,

        # Specifies a name for the entry.
        [Parameter(Mandatory = $true)]
        [ValidateNotNullorEmpty()]
        [String]
        $Name,

        # Specifies the property value of the entry.
        [Parameter(Mandatory = $true)]
        [ValidateNotNullorEmpty()]
        [String]
        $Value,

        # Specifies the type of property that this cmdlet adds (if needed). The acceptable values for this parameter are: String. Specifies a null-terminated string. Equivalent to REG_SZ. - ExpandString. Specifies a null-terminated string that contains unexpanded references to environment variables that are expanded when the value is retrieved. Equivalent to REG_EXPAND_SZ. - Binary. Specifies binary data in any form. Equivalent to REG_BINARY. - DWord. Specifies a 32-bit binary number. Equivalent to REG_DWORD. - MultiString. Specifies an array of null-terminated strings terminated by two null characters. Equivalent to REG_MULTI_SZ. - Qword. Specifies a 64-bit binary number. Equivalent to REG_QWORD. - Unknown. Indicates an unsupported registry data type, such as REG_RESOURCE_LIST.
        [Parameter(Mandatory = $true)]
        [ValidateNotNullorEmpty()]
        [String]
        $PropertyType
    )
    # Test if the path exists
    if (-not (Test-Path -Path $Path)) {
        Write-Verbose -Message 'Path does not exist, Calling New-Item'
        if ($PSCmdlet.ShouldProcess($Name, 'New-Item')) {
            $null = New-Item $Path -ItemType Directory
        }
    }

    $Item = Get-Item -Path $Path

    # Test to see if the item exists, if it does, then we set the value, if it does not, we call new-value
    if ($Item.Property.Contains($Name)) {
        Write-Verbose -Message 'Item Exists, Calling Set-ItemProperty'
        if ($PSCmdlet.ShouldProcess($Name, 'Set-ItemProperty')) {
            $null = Set-ItemProperty -Path $Path -Name $Name -Value $Value
        }
    } else {
        Write-Verbose -Message 'Item Does not Exist, Calling New-ItemProperty'
        if ($PSCmdlet.ShouldProcess($Name, 'New-ItemProperty')) {
            $null = New-ItemProperty -Path $Path -Name $Name -PropertyType $PropertyType -Value $Value
        }
    }

}