пятница, 24 октября 2008 г.

Create Registry Path in PowerShell


Here is script to create registry path (or separate key) in PowerShell



function vrIsRegistryPathContainsKey($basePath, $key)
{
if($key.Length -le 0)
{ return 0 }

$childItems = Get-ChildItem $basePath -Force -ErrorAction SilentlyContinue
foreach ($childItem in $childItems)
{
if($childItem.name -match $key)
{
return 1
}
}
return 0
}

function vrCreateRegistryPath($basePath, $createdPath)
{
$pathTokens = $createdPath.split("\")
foreach ($pathToken in $pathTokens)
{
if($pathToken.Length -le 0)
{ break }

if(-not(vrIsRegistryPathContainsKey $basePath $pathToken))
{
echo ('not found ' + $pathToken);
New-Item -Path $basePath -Name $pathToken
}
$basePath = $basePath + "\" + $pathToken
echo ('basepath ' + $basePath);
}
}


Usage:
vrCreateRegistryPath 'Microsoft.PowerShell.Core\Registry::\HKEY_LOCAL_MACHINE\SOFTWARE' 'Key\SubKey1\Subkey2\'