2020 | Scott W. Head  |  Sr. Systems Admin | Certified Microsoft Windows Server Administrator

PowerShell Registry Key Get

<#
    ScriptByScott.com
    
    .Synopsis
    Scott Head | 11/28/2018 12:33:03 PM
    .DESCRIPTION
        Gets a list of servers from source text file. Tests the path to the key and pulls key value.
        Returns the Key Value and the ComputerName
        Also has error tapping built in and error messages.

    .EXAMPLE
        Script has to ran with elevated rights on the Domain / Loca Admin Rights to access all servers.

         Mandatory File - C:\temp\ServerList.txt
         Mandatory Dir  - C:\temp
    #>

    $Server=Get-Content C:\temp\ServerList.txt

    $MyCommand={    

        #||||||||||||||||||||| Set Inital Key Path ||||||||||||||||||||
        $Path1="HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\Clients\Mail\Hotmail"

        #||| Test Key Path |||
        If(Test-Path $Path1){
            #||| Error Trapping |||
            Try{

                    #|||||||||||||||||||||||||||||||| Get Value for Specified Key |||||||||||||||||||||||||||||
                    $MyKeyReturned=Get-ItemProperty $Path1 | Select Object -ExpandProperty "LocalizedString" -ErrorAction stop

                    #||||| Set up old School Object for Backward Compatibilty to 2.0 for Returning Data ||||||
                    $Obj=New-Object PSObject 
                    $Obj |Add-Member NoteProperty ServerName ($Env:Computername)
                    $Obj |Add-Member NoteProperty ReturnedKey ($MyKeyReturned)
                    Return $Obj  

                }Catch{

                    #||||| Set up old School Object for Backward Compatibilty to 2.0 for Returning Data ||||||
                    $Obj=New-Object PSObject 
                    $Obj |Add-Member NoteProperty ServerName ($Env:Computername)
                    $Obj |Add-Member NoteProperty ReturnedKey ($_.Exception)
                    Return $Obj 


                }
        }else{

                    #||||| Set up old School Object for Backward Compatibilty to 2.0 for Returning Data ||||||
                    $Obj=New-Object PSObject 
                    $Obj |Add-Member NoteProperty ServerName ($Env:Computername)
                    $Obj |Add-Member NoteProperty ReturnedKey ("Key Access-Failed")
                    Return $Obj 
        }

    }

        #||| Error Trap |||||
        Try{

           Invoke-Command -ComputerName $Server -ScriptBlock $MyCommand -ErrorAction Stop | Select-Object -Property * -ExcludeProperty RunspaceId, PSComputerName

        }Catch{

            $_.Exception

        }