Reg Value

Ask your 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.
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
PS12018
Posts: 7
Last visit: Wed Aug 01, 2018 7:19 am

Reg Value

Post 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
.......
cody m

Re: Reg Value

Post 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>
User avatar
PS12018
Posts: 7
Last visit: Wed Aug 01, 2018 7:19 am

Re: Reg Value

Post 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
.......
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Reg Value

Post by jvierra »

What key are you trying to query?

Start by reading the help for the registry provider:

help registry
User avatar
PS12018
Posts: 7
Last visit: Wed Aug 01, 2018 7:19 am

Re: Reg Value

Post 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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Reg Value

Post by jvierra »

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 »

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
Last visit: Wed Aug 01, 2018 7:19 am

Re: Reg Value

Post 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}
User avatar
PS12018
Posts: 7
Last visit: Wed Aug 01, 2018 7:19 am

Re: Reg Value

Post 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Reg Value

Post 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
}
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked