Link to home
Start Free TrialLog in
Avatar of Isaiah Melendez
Isaiah Melendez

asked on

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

Avatar of footech
footech
Flag of United States of America image

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

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial