Search code examples
powershellscriptingautomation

Why my PowerShell script is not respecting the steps order?


I tried to find the solution by myself but now I'm out of ideas. I wrote a script that I want to use to check if Erlang is installed on a machine:

# Check if a Software ins installed
function Check_Program_Installed($programName) {
$x86_check = ((Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall") |
Get-ItemProperty |
        Where-Object {$_.DisplayName -like "*$programName*" } |
            Select-Object -Property DisplayName, UninstallString)

if(Test-Path 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall')  
{
$x64_check = ((Get-ChildItem "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall") |
Get-ItemProperty |
        Where-Object {$_.DisplayName -like "*$programName*" } |
            Select-Object -Property DisplayName, UninstallString)
}
if ($x86_check -and $x64_check -eq $null){ 
    write-host "$programName is not installed on this computer" -ForegroundColor Green 
    #continue
    }
elseif ($x86_check -or $x64_check -ne $null){
    write-host "On this computer is installed " -ForegroundColor Red     
    $x86_check
    $x64_check

    }
}

# Erlang check
Write-Host "Checking if Erlang exist    " -NoNewline
Check_Program_Installed("Erlang")

Write-Host "The End: the script ends here" -ForegroundColor Yellow

But if I execute it as result the very last line is executed before Check_Program_Installed("Erlang"). Why PowerShell is not respecting the step priority?

enter image description here

Checking if Erlang exist        On this computer is installed

The End: the script ends here
DisplayName          UninstallString
-----------          ---------------
Erlang OTP 21 (10.2) C:\Program Files\erl10.2\Uninstall.exe

Solution

  • Just add Format-Table to $x86_check and $64_check at the end. Link to answer.

    # Check if a Software ins installed
    function Check_Program_Installed($programName) {
    $x86_check = ((Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall") |
    Get-ItemProperty |
            Where-Object {$_.DisplayName -like "*$programName*" } |
                Select-Object -Property DisplayName, UninstallString) | Format-Table
    
    if(Test-Path 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall')  
    {
    $x64_check = ((Get-ChildItem "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall") |
    Get-ItemProperty |
            Where-Object {$_.DisplayName -like "*$programName*" } |
                Select-Object -Property DisplayName, UninstallString) | Format-Table
    }
    if ($x86_check -and $x64_check -eq $null){ 
        write-host "$programName is not installed on this computer" -ForegroundColor Green 
        #continue
        }
    elseif ($x86_check -or $x64_check -ne $null){
        write-host "On this computer is installed " -ForegroundColor Red     
        $x86_check
        $x64_check
    
        }
    
    }
    
    # Erlang check
    Write-Host "Checking if Erlang exist    " -NoNewline
    Check_Program_Installed("Erlang")
    Write-Host "The End: the script ends here" -ForegroundColor Yellow