Page 1 of 2

Reg Value

Posted: Mon Jun 25, 2018 5:57 am
by PS12018
I am trying to write a script the would read the value of a Reg key, but I cant seem to get it to work correctly. Any help is appreciated.

The key I would like to read.
Productname Windows xp

$val = Get-Item -path $reg | select-object -expandproperty Productname
$win = "Windows xp"

if ($val -eq $win)
......
else
.......

Re: Reg Value

Posted: Mon Jun 25, 2018 8:20 am
by cody m
You're going to want to use the Get-ItemProperty cmdlet to get information from the registry.

Code: Select all

Get-ItemProperty -path <Path to reg> -Name <Reg key Name>

Re: Reg Value

Posted: Mon Jun 25, 2018 9:29 am
by PS12018
Thanks for the reply...
I made the switch, but it still results in the "else" when it shouldn't. any ideas?


$val = Get-ItemProperty -path $reg -name Productname

$win = "Windows xp"

if ($val -eq $win)
......
else
.......

Re: Reg Value

Posted: Mon Jun 25, 2018 9:40 am
by jvierra
What key are you trying to query?

Start by reading the help for the registry provider:

help registry

Re: Reg Value

Posted: Mon Jun 25, 2018 12:11 pm
by PS12018
I am trying to query the Registry Data value. Within a Registry key. For example Hkey_local_machine\software\Microsoft\window NT\CurrentVersion: you will find a Reg key name Productname with the Data Value of your OS version(window XP , window 7, etc.). I would like to read that value against an If statement.

Re: Reg Value

Posted: Mon Jun 25, 2018 1:32 pm
by jvierra
Are you trying to do this?

Get-ItemProperty 'HKLM:\software\Microsoft\Windows NT\CurrentVersion\' -Name ProductName

Re: Reg Value

Posted: Mon Jun 25, 2018 2:00 pm
by cody m
Get-ItemProperty returns an object for each item property that it gets. So you are getting back a PSCustomObject when you run the cmdlet, to get just the productname information you need to access that property. Try the code below and check to see if you are getting the product name.

Code: Select all

$VersionInfo = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\" -Name "ProductName"
$VersionInfo.ProductName

Re: Reg Value

Posted: Tue Jun 26, 2018 5:59 am
by PS12018
Thanks for the reply. I tried it and still the same results, I may be missing something. Below is the script I am using to test it. I am on an XP system so Calc should start.

$VersionInfo = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\" -Name "ProductName"
$VersionInfo.ProductName

$win = "Windows xp"

if ($val -eq $win)
{Calc}
else
{notepad}

Re: Reg Value

Posted: Tue Jun 26, 2018 6:01 am
by PS12018
jvierra wrote: Mon Jun 25, 2018 1:32 pm Are you trying to do this?

Get-ItemProperty 'HKLM:\software\Microsoft\Windows NT\CurrentVersion\' -Name ProductName
This was the first thing I tried and wasn't getting the results I needed

Re: Reg Value

Posted: Tue Jun 26, 2018 6:50 am
by jvierra
I think you need to start by learning basic PowerShell. Without some training all of this will seem quite impossible.

Code: Select all

if('Windows xp' -eq (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name ProductName).ProductName){
    Calc
}else{
    notepad
}