Wednesday, February 25, 2015

PowerShell - Read, Write and Access Registry

The registry provider made it easy to work with the registry
on a local machine or a remote server to load the registry as a PSDrive and
work on it. By default, the Registry provider creates two registry drives HKCU
and HKLM. If you want to load other drives, you need to use the New-PSDrive
cmdlet to achieve this. This simplifies accessing data in the registry using
PowerShell, you can now access the registry as if it were a file system and use
the same cmdlets as working with files and directories like Get-Item, Set-Item
etc.

We'll make use of this feature to add/ retrieve values from
the registry. I've created some functions to check whether a value exists, get
the value and set a value in a registry path, name combination.

Function
Test-RegistryValue

{

[CmdletBinding()]

param

(

[Parameter(Mandatory
= $true)]

[ValidateNotNullOrEmpty()]

[String]$Path,

[Parameter(Mandatory
= $true)]

[ValidateNotNullOrEmpty()]

[String]$Name

)

if
(Test-Path$Path)

{

$key
= Get-Item -LiteralPath $Path

if
($key.GetValue($name,
$null) -ne $null)

{

$true

}

else

{

$false

}

}

else

{

$false

}

}

Function
Get-RegistryValue

{

[CmdletBinding()]

param

(

[Parameter(Mandatory
= $true)]

[ValidateNotNullOrEmpty()]

[String]$Path,

[Parameter(Mandatory
= $true)]

[ValidateNotNullOrEmpty()]

[String]$Name

)

if
(Test-RegistryValue -Path $Path
-Name $Name)

{

$key
= Get-Item -LiteralPath $Path

$key.GetValue($name,
$null)

}

else

{

$null

}

}

Function
Set-RegistryValue

{

[CmdletBinding()]

param

(

[Parameter(Mandatory
= $true)]

[ValidateNotNullOrEmpty()]

[String]$Path,

[Parameter(Mandatory
= $true)]

[ValidateNotNullOrEmpty()]

[String]$Name,

[Parameter(Mandatory
= $true)]

[ValidateNotNull()]

$Value

)

if
(Test-RegistryValue -Path $Path
-Name $Name)

{

$key
= Get-Item -LiteralPath $Path

$key.SetValue($name,
$Value)

}

}

To test the scripts I’ve used the FluentShellUnit project
and have my test cases as