Reg Value

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
PS12018
Posts: 7
Joined: Tue Apr 24, 2018 7:57 am

Reg Value

Post by PS12018 » Mon Jun 25, 2018 5:57 am

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
.......

cody m

Re: Reg Value

Post by cody m » Mon Jun 25, 2018 8:20 am

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>

User avatar
PS12018
Posts: 7
Joined: Tue Apr 24, 2018 7:57 am

Re: Reg Value

Post by PS12018 » Mon Jun 25, 2018 9:29 am

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
.......

User avatar
jvierra
Posts: 13732
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Reg Value

Post by jvierra » Mon Jun 25, 2018 9:40 am

What key are you trying to query?

Start by reading the help for the registry provider:

help registry

User avatar
PS12018
Posts: 7
Joined: Tue Apr 24, 2018 7:57 am

Re: Reg Value

Post by PS12018 » Mon Jun 25, 2018 12:11 pm

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.

User avatar
jvierra
Posts: 13732
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Reg Value

Post by jvierra » Mon Jun 25, 2018 1:32 pm

Are you trying to do this?

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

cody m

Re: Reg Value

Post by cody m » Mon Jun 25, 2018 2:00 pm

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

User avatar
PS12018
Posts: 7
Joined: Tue Apr 24, 2018 7:57 am

Re: Reg Value

Post by PS12018 » Tue Jun 26, 2018 5:59 am

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}

User avatar
PS12018
Posts: 7
Joined: Tue Apr 24, 2018 7:57 am

Re: Reg Value

Post by PS12018 » Tue Jun 26, 2018 6:01 am

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

User avatar
jvierra
Posts: 13732
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Reg Value

Post by jvierra » Tue Jun 26, 2018 6:50 am

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
}

Locked