Powershell Script - v3.0 - Assistance

Hello experts,

I am trying to build a conditional statement that does if-else in powershell.

What I want it to do is query a registry path, if it exists print out do nothing

else, create the registry path.

This is what I have...

 $ChkFile = (Get-ItemProperty "HKCU:\SOFTWARE\Progressive Automotive Systems\R.O. Writer") | Select-Object -ExpandProperty DataFile
$FileExists = Test-Path $ChkFile 
If ($FileExists -eq $True) {
Write-Host "This regsitry key exists"
}
else {Write-Host "Hello, World"} 

Open in new window

sj77Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
For the registry, Test-Path can only be used for keys, not values.  So you could test for the existence of the key "HKCU:\SOFTWARE\Progressive Automotive Systems\R.O. Writer" using the cmdlet, but not any values in the key location.

One way to handle testing for a registry value is to detect the error from Get-ItemProperty.
try {
    $ChkFile = Get-ItemProperty "HKCU:\SOFTWARE\Progressive Automotive Systems\R.O. Writer" -Name "DataFile" -ErrorAction Stop | Select-Object -ExpandProperty DataFile
    Write-Host "This registry value exists"
}
catch {
    "Registry path doesn't exist"
}

Open in new window

0
oBdACommented:
This will test and create both the key and the new value:
$Key = "HKCU:\SOFTWARE\Progressive Automotive Systems\R.O. Writer"
$Name = 'DataFile'
$Value = "C:\Temp\test.txt"

If (Test-Path -Path $Key) {
	"Key '$Key' found." | Write-Host
} Else {
	"Key '$Key' not found, will create ..." | Write-Host
	New-Item -Path $Key -Force | Out-Null
}
$DataFile = Get-ItemProperty -Path $Key -ErrorAction SilentlyContinue | Select-Object -ExpandProperty $Name
If ([string]::IsNullOrEmpty($DataFile)) {
	"Registry value '$($Name)' not found, will create and set to '$($Value)' ..." | Write-Host
	New-ItemProperty -Path $Key -Name $Name -Value $Value -Force | Out-Null
	$DataFile = $Value
} Else {
	"Found registry value '$($Name): '$($DataFile)'" | Write-Host
}
"DataFile: '$($DataFile)'" | Write-Host

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.