Powershell if exist

Hi can some one help with this.
need a script to check the registry and if a value exists carry on and run rest of script if reg entry doesnt exist error out

so sccm wont run if regentry isnt present

this is an edit of something that might work but it doesnt :-)

if ((Test-Path "hklm:\Software\mch\$Manufacturer\$Product\$Ver") -eq $false) 
	$reg=Get-Itemproperty $RegPath
	if ($reg.Media_Reference -eq $installmediaid) {wegotaproblem "$Product is not installed"}

Open in new window

Who is Participating?
Chris DentPowerShell DeveloperCommented:

You can stop a script with "Break", or you can throw an error (Throw "Because of Some Error").

In the snippet above, you're testing a path to see if it exists. Then if it doesn't you're attempting to get a value from the path... which is a bit of an unusual choice...

mhamerAuthor Commented:
im editing an entry from another script that checks version number and does the opposite it exits if already installed
Chris DentPowerShell DeveloperCommented:

It was only really this bit that's troubling :)

(Test-Path TheRegKey) -eq $false

Because it's followed with "Get-Item TheRegKey" which only runs if the key doesn't exist which will break. A bit backwards you see.

mhamerAuthor Commented:
the code i posted was relying on other stuff elseware in script  so it makes sense when you see the whole, but just breaking was fine for what i was needing  thanks
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.