Created
November 29, 2018 11:05
-
-
Save nohwnd/caa259289c20f46923132ae70640d9f7 to your computer and use it in GitHub Desktop.
Sets registry keys to all users present on the computer, including current and default user
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #requires -RunAsAdministrator | |
| param ( | |
| [Parameter(Mandatory)] | |
| $Path) | |
| $ErrorActionPreference = 'stop' | |
| if (-not (Test-Path $Path)) {throw "Path $Path does not exist!" } | |
| # import it for current user | |
| $Path = Resolve-Path $Path | |
| reg.exe import $Path | |
| $content = Get-Content $Path | |
| $file = Join-Path $env:temp ((New-Guid).ToString('N') + '.reg') | |
| # import it for new users | |
| $updatedContent = $content -replace '\[(-?)HKEY_CURRENT_USER', "[`$1HKEY_USERS\.DEFAULT" | |
| Set-Content -Value $updatedContent -Path $file -Force | |
| reg.exe import $file | |
| # import it for all other users | |
| $mountedHive = 'HKEY_USERS\mounted' | |
| $updatedContent = $content -replace '\[(-?)HKEY_CURRENT_USER', "[`$1$mountedHive" | |
| Set-Content -Value $updatedContent -Path $file -Force | |
| Get-Item C:\users\*\ntuser.dat -Force | % FullName | | |
| ? { $_ -notlike "*\users\$($env:USERNAME)\*" } | | |
| foreach { | |
| try { | |
| "Writing $_" | |
| reg.exe load $mountedHive $_ | |
| reg.exe import $file | |
| reg.exe unload $mountedHive | |
| } catch { | |
| $ErrorActionPreference = 'continue' | |
| Write-Error $_ | |
| } | |
| } | |
| Remove-Item $file -Force |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment