# Should this function return a value other than null ?

Hello,

I am puzzle over what is expected to return from this sample function below. It should return to me some value other than null which is in fact returning. If you could shed some light into what’s going on , please let me know.

Below is the simple code that I got from Google and I’m trying to understand of why it returns a null value on the reg value.

\$gprRegPath = “HKLM:\Software\Microsoft\Windows\CurrentVersion”
function Get-Reg(\$key, \$val) {

if(\$key -and !\$val) { return test-path \$key }
elseif(\$key) { write-host “Key is \$key”

return (gp \$key -ea silentlycontinue).\$val }
else { help get-reg }
}

##get-reg \$gprRegPath Domain
\$reg = GET-REG \$gprRegPath Domain
write-host “Reg is \$reg”

What is returned when you run the base registry query?

gp "HKLM:\Software\Microsoft\Windows\CurrentVersion"

Is there a ‘Domain’ entry in the registry you’re querying ?

ProgramFilesDir : C:\Program Files
CommonFilesDir : C:\Program Files\Common Files
ProgramFilesDir (x86) : C:\Program Files (x86)
CommonFilesDir (x86) : C:\Program Files (x86)\Common Files
CommonW6432Dir : C:\Program Files\Common Files
DevicePath : C:\WINDOWS\inf
MediaPathUnexpanded : C:\WINDOWS\Media
ProgramFilesPath : C:\Program Files
ProgramW6432Dir : C:\Program Files
SM_ConfigureProgramsName : Set Program Access and Defaults
SM_GamesName : Games
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows
PSChildName : CurrentVersion
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry

What are you expecting from this piece of code? In-fact there is no property called ‘Domain’ in the given key, that’s why it is returning null

[pre]

Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion

ProgramFilesDir : C:\Program Files
CommonFilesDir : C:\Program Files\Common Files
ProgramFilesDir (x86) : C:\Program Files (x86)
CommonFilesDir (x86) : C:\Program Files (x86)\Common Files
CommonW6432Dir : C:\Program Files\Common Files
DevicePath : C:\Windows\inf
MediaPathUnexpanded : C:\Windows\Media
ProgramFilesPath : C:\Program Files
ProgramW6432Dir : C:\Program Files
SM_ConfigureProgramsName : Set Program Access and Defaults
SM_GamesName : Games
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows
PSChildName : CurrentVersion
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry

[/pre]

Thank you so much Kiran for sheding lights into my confusion.

As I ran gp “HKLM:\Software\Microsoft\Windows\CurrentVersion”

It returns :

ProgramFilesDir : C:\Program Files
CommonFilesDir : C:\Program Files\Common Files
ProgramFilesDir (x86) : C:\Program Files (x86)
CommonFilesDir (x86) : C:\Program Files (x86)\Common Files
CommonW6432Dir : C:\Program Files\Common Files
DevicePath : C:\Windows\inf
MediaPathUnexpanded : C:\Windows\Media
ProgramFilesPath : C:\Program Files
ProgramW6432Dir : C:\Program Files
SM_ConfigureProgramsName : Set Program Access and Defaults
SM_GamesName : Games
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows
PSChildName : CurrentVersion
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry

Could I ask what do you mean by if there is a Domain entry in the registry that I am querying ? Based on what I got from the above returned, I could not find

any Domain entry. Please correct me if I was looking at the right place or should I look somewhere else for the Domain entry and how ?

Many Thanks for all of the help!

When you call a property of an object, first it should exist otherwise it returns a null value.

By the way, what is your requirement?

Thank you so much Kiran for all of the helps and explanations.

I undestand now why I got a null value because of like you were explaining that there is no property called “Domain” in the given

“HKLM:\Software\Microsoft\Windows\CurrentVersion” key; so that’s why it gave me a null value.

Furthere more, gp “HKLM:\Software\Microsoft\Windows\CurrentVersion” show me that there is no “Domain” property.