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
.......
Reg Value
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.
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.
Re: Reg Value
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
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
.......
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
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
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
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}
$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
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
}