Search code examples
powershellregistrykey

Script to read and compare registry entries


I am very new to PowerShell and I am getting stuck on this question.

  • Read the registry entries from both of the locations named in the project description.
    HKEY_CURRENT_USER/Software/Microsoft/Windows/CurrentVersion/Run HKEY_LOCAL_MACHINE/Software/Microsoft/Windows/CurrentVersion/Run
  • Compare each entry to a list of acceptable entries. The acceptable entry list is from a text file named "Acceptable_Reg.txt" that will accompany the script when the script is downloaded.
  • Produce a text file report that lists all unacceptable registry entries. Save the report using the computer name as the file name.
  • Transmit the report file to the following intranet address: intranet.xyzcompany.com/bad_reg.aspx

I have come up with this so far...but I don't think this is the right way to go about it. I know I will probably need to use the compare-object cmdlet but am unsure how to apply it.

$path1 ="HKCU:\Software\Microsoft\Windows\CurrentVersion\Run"
$path2 = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Run
$destination = "(FileName.txt)"
$results = Get-ItemProperty $path1 $path2

Any help on this would be greatly appreciated because my professor is unable to provide me any type of help with the actual script.


Solution

  • For comparing if you have exported the reg files, you can do:

    $location1 = "C:\temp\location1";
    $location2 = "C:\temp\location2";
    $location3 = "C:\temp";
    
    Compare-Object $(Get-Content "$location1\file1.reg") $(Get-Content "$location2\file2.reg") | 
        Where-Object { IsNotAccepted($_.InputObject) } | 
        Out-File "$location3\NotAcceptedEntries.txt" -Force
    
    function IsNotAccepted($entry){
        $accepted = $false;
        # $name = $entry.Split('=')[0]
        # $value = $entry.Split('=')[1]
    
        # Put your logics here
    
        return -Not($accepted);
    }
    

    Note that $_.InputObject represents the entry, e.g.

    "C:\Program Files (x86)\7-Zip\7z.exe.FriendlyAppName"="7-Zip Console 2"

    You also can know in which file was that by checking $_.SideIndicator. If it returns => it means right file (file2.reg) has that difference. Conversely <= means the difference was found in left file (file1.reg).