Skip to content

Instantly share code, notes, and snippets.

@d4rkeagle65
Last active December 1, 2020 15:35
Show Gist options
  • Save d4rkeagle65/c2b1ffac59b9b0e5ba0ffc6eb645d201 to your computer and use it in GitHub Desktop.
Save d4rkeagle65/c2b1ffac59b9b0e5ba0ffc6eb645d201 to your computer and use it in GitHub Desktop.
# An example template powershell script that will allow you to work with the user registry keys for all users
# on a system or terminal server. It will check if the ntuser.dat file is mounted, and if not mount it with the
# users SID. If the ntuser.dat file was one mounted by the script (ie, user is not logged in), it will unmount
# the dat file after.
# This script uses the example of querying and displaying the set default printer per user for all users on a
# system.
$users = Get-WMIObject Win32_UserAccount
$hku = New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS
$users | Foreach-Object {
$user = $_
$ntdat_mounted = $null
If (!($user.Caption -like '*Administrator')) {
If (Test-Path ('HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\' + $user.SID) -ErrorAction SilentlyContinue) {
$profileImagePath = (Get-ItemProperty -Path ('HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\' + $user.SID)).ProfileImagePath
# Check if Registry Keys already Loaded. Load if not loaded.
If (!(Test-Path ('HKU:\' + $user.SID) -ErrorAction SilentlyContinue)) {
If (Test-Path ($profileImagePath + '\ntuser.dat') -ErrorAction SilentlyContinue) {
$ntdat_mounted = $true
reg load ('HKU\' + $user.SID) ($profileImagePath + '\ntuser.dat') | Out-Null
}
}
If (Test-Path ('HKU:\' + $user.SID) -ErrorAction SilentlyContinue) {
$defaultPrinter = (Get-ItemProperty -Path ('HKU:\' + $user.SID + '\Software\Microsoft\Windows NT\CurrentVersion\Windows\')).Device
Write-Host ("User:[" + $user.FullName + "] DefaultPrinter:[" + $defaultPrinter + "]")
}
# Unload Registry Key if Loaded
If ($ntdat_mounted) {
reg unload ('HKU\' + $user.SID) | Out-Null
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment