Skip to main content

Use powershell to track Windows Registry changes


marcofabbri
Forum|alt.badge.img+13

Did you know that you can use Powershell to track registry changes? Well, it does.

There’s a function that create a snapshot of the current HKLM and HKCU registry keys to use them to compare with future snapshots. To create a Windows Registry snapshots, use the following PowerShell commands in a Windows PowerShell (Admin) prompt to make sure you can access all of the registry keys:

 

dir -rec -erroraction ignore HKLM:\ | % name > Base-HKLM.txt

dir -rec -erroraction ignore HKCU:\ | % name > Base-HKCU.txt

 

It’ll be generated a Base-HKLM.txt and Base-HKCU.txt

On freshly installed versions of Windows 11 and Windows 10, these snapshots’ sizes are about:

HKEY_LOCAL_MACHINE (HKLM): 82 MB
HKEY_CURRENT_USER (HKCU): 3 MB

 

After some time, if you need to compare the Windows registry with your base snapshots, you can create new snapshots using these following commands in an admin PowerShell prompt:

dir -rec -erroraction ignore HKLM:\ | % name > Current-HKLM-$(get-date -f dd-MM-yyyy).txt

dir -rec -erroraction ignore HKCU:\ | % name > Current-HKCU-$(get-date -f dd-MM-yyyy).txt

 

Now that you have both base snapshots and current snapshots created, you can compare them using another powershell command:

Compare-Object (Get-Content -Path .\Base-HKCU.txt) (Get-Content -Path .\[last_updated_snapshot.txt])

 

And you got what registry keys got added or changed, but only names, not values too.

 

 

It’s another way to use this command:

Get-ItemProperty -Path HKLM:\* | select pschildname

 

More info: https://docs.microsoft.com/en-us/powershell/scripting/samples/working-with-registry-keys?view=powershell-7.2

 

 

10 comments

Chris.Childerhose
Forum|alt.badge.img+21

Very cool and interesting. Always looking at ways to audit and improve security.  Will definitely test this out. 👍


Iams3le
Forum|alt.badge.img+11
  • Veeam Legend
  • 1393 comments
  • May 4, 2022

Great stuff @marcofabbri I will explore this and test it. Thank you for sharing.


Link State
Forum|alt.badge.img+11
  • Veeam Legend
  • 608 comments
  • May 5, 2022

hi thx @marcofabbri 

I link 10 powershell commands for security analyst 😊

10 PowerShell Security Scripts for Analyst and Administrators (filecloud.com)


Chris.Childerhose
Forum|alt.badge.img+21
Link State wrote:

hi thx @marcofabbri 

I link 10 powershell commands for security analyst 😊

10 PowerShell Security Scripts for Analyst and Administrators (filecloud.com)

These are very cool too.  Thanks for sharing.


dips
Forum|alt.badge.img+7
  • Veeam Legend
  • 808 comments
  • May 6, 2022

Additionally, the snapshots could be saved in Git for each workstation and could be easily diff’ed at a future time. 


Link State
Forum|alt.badge.img+11
  • Veeam Legend
  • 608 comments
  • May 10, 2022

Rick Vanover
Forum|alt.badge.img+10

This is really interesting @marcofabbri  thanks for highlighting.


Iams3le
Forum|alt.badge.img+11
  • Veeam Legend
  • 1393 comments
  • May 10, 2022

vNote42
Forum|alt.badge.img+13
  • On the path to Greatness
  • 1246 comments
  • May 11, 2022

Cool finding! Key is the compare-object cmdlet, I didn’t know up to now.


BertrandFR
Forum|alt.badge.img+8
  • Influencer
  • 528 comments
  • May 20, 2022

Interesting topic, we have an nrpe check who check a baseline from hardening windows OS. We tracked changes in registry and secpol… I will share this with my windows team 😁


Comment