Search code examples
powershellif-statementstatements

How to use IF statements, that if met proceed to a part in the powershell script


I have written a Powershell script that installs a bunch of features and roles in Server 2012 and the script works perfectly, but I need help figuring out how to check if UAC is enabled, if it is, to prompt that the server will restart when they press enter or if it is not, then to proceed to the next line, or the next functioning line where the script really starts.

So I have this line that checks to see if UAC is enabled or diabled

 (Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System).EnableLUA

If UAC is disabled, it prints a 0 and if UAC is enabled it prints a 1.

If it is disabled, I would like it to continue to a line in the script that reads

Start-Transcript

If it is enabled, it reads 1 and I would like it to proceed to this line

New-ItemProperty -Path HKLM:Software\Microsoft\Windows\CurrentVersion\policies\system -Name EnableUA -PropertyType DWord -Value 0 -Force
Restart-Computer

Am I going about this the wrong way?


Solution

  • if ( (Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System).EnableLUA -eq 0 )
    {
        Start-Transcript
    }
    else
    {
        New-ItemProperty -Path HKLM:Software\Microsoft\Windows\CurrentVersion\policies\system -Name EnableUA -PropertyType DWord -Value 0 -Force
        Restart-Computer
    }