Home > Enterprise >  How do I add a variable into a specific registry key path via Powershell?
How do I add a variable into a specific registry key path via Powershell?

Time:04-12

I currently have a script which looks for a specific program to exist in Add/Remove programs and I would like to add the "Dell Command" into registry to confirm.

#Find the Dell Command Update in Add/Remove Programs
Get-ChildItem HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall | 
% {Get-ItemProperty $_.PsPath} | 
where {$_.Displayname -match "Dell Command"} |
sort Displayname | select DisplayName

CodePudding user response:

If you would like to create a key based on the value returned from your search, you can do the following:

$dellCommand = (Get-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" | Where-Object -Property "DisplayName" -Match "Dell Command").DisplayName
$regKeyPath  = "HKLM:\SOFTWARE\$dellCommand"
    if (-not (Test-Path -Path $regKeyPath)) {
        Write-Output -InputObject "$regKeyPath doesn't exist. Creating key."
        $newKey = New-Item -Path $regKeyPath -Force  #Create the key if it doesn't exist.

        #Add Property values to the key
        #$newKey | New-ItemProperty -Name "blah" -Value "valuevalue" -PropertyType "type" -Force 
    }
    else {
        Write-Output -InputObject "Key already exists: [$dellCommand]."
    }

Here, you can test to see if the key already exists and create it if it doesn't. Since New-Item produces an output of the object, you can use that to pipe into New-ItemProperty if needed to add properties to the key (demonstrated above).

  • Related