如何使用Powershell将.reg文件导入计算机上的每个用户个人资料?

时间:2019-07-10 15:54:28

标签: powershell registry

我想将.reg文件导入计算机上的每个用户个人资料。

我还想知道如何将.reg文件存储为变量,并使用该变量来更改reg键。

该reg文件有太多行要单独添加。

$PatternSID = 'S-1-5-21-\d+-\d+\-\d+\-\d+$'
 Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\*' | Where-Object {$_.PSChildName -match $PatternSID} |
     select  @{name="SID";expression={$_.PSChildName}},
             @{name="UserHive";expression={"$($_.ProfileImagePath)\ntuser.dat"}},
             @{name="Username";expression={$_.ProfileImagePath -replace '^(.*[\\\/])', ''}}

Get-ChildItem Registry::HKEY_USERS | Where-Object {$_.PSChildName -match $PatternSID} | select PSChildName

# Regex pattern for SIDs
$PatternSID = 'S-1-5-21-\d+-\d+\-\d+\-\d+$'

# Get Username, SID, and location of ntuser.dat for all users
$ProfileList = gp 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\*' | Where-Object {$_.PSChildName -match $PatternSID} | 
    Select  @{name="SID";expression={$_.PSChildName}}, 
            @{name="UserHive";expression={"$($_.ProfileImagePath)\ntuser.dat"}}, 
            @{name="Username";expression={$_.ProfileImagePath -replace '^(.*[\\\/])', ''}}

# Get all user SIDs found in HKEY_USERS (ntuder.dat files that are loaded)
$LoadedHives = gci Registry::HKEY_USERS | ? {$_.PSChildname -match $PatternSID} | Select @{name="SID";expression={$_.PSChildName}}

# Get all users that are not currently logged
$UnloadedHives = Compare-Object $ProfileList.SID $LoadedHives.SID | Select @{name="SID";expression={$_.InputObject}}, UserHive, Username

# Loop through each profile on the machine
Foreach ($item in $ProfileList) {
    # Load User ntuser.dat if it's not already loaded
    IF ($item.SID -in $UnloadedHives.SID) {
        reg load HKU\$($Item.SID) $($Item.UserHive) | Out-Null
    }

    #####################################################################
    # This is where you can read/modify a users portion of the registry 

    #####################################################################
    #I WANT TO IMPORT THESE REG SETTINGS FOR EACH USER, BUT NOT SURE HOW TO FORMAT. HELP?
    reg import "\\fakecompanyname.com\files\public\IT\Protected\Projects\SOLIDWORKS 2019 deployment\fakeregfilename.sldreg"

    #####################################################################
    # This example lists the Uninstall keys for each user registry hive (and was the example from where I copied the rest of the code, I don't need this, just including here for reference)
    #"{0}" -f $($item.Username) | Write-Output
    #Get-ItemProperty registry::HKEY_USERS\$($Item.SID)\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | 
    #    Foreach {"{0} {1}" -f "   Program:", $($_.DisplayName) | Write-Output}
    #Get-ItemProperty registry::HKEY_USERS\$($Item.SID)\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | 
    #    Foreach {"{0} {1}" -f "   Program:", $($_.DisplayName) | Write-Output}

    #####################################################################

    # Unload ntuser.dat        
    IF ($item.SID -in $UnloadedHives.SID) {
        ### Garbage collection and closing of ntuser.dat ###
        [gc]::Collect()
        reg unload HKU\$($Item.SID) | Out-Null
    }
}

有点棘手的测试。

我们最终使用...

    # read in reg file with the settings we need
    $regstuff = Get-Content "\\fakecomapnyname.com\files\public\IT\Protected\Projects\SOLIDWORKS-2019-deployment\fakesettings.reg"

    # Fix it so it applies to the specific profile for this loop, profile
    $regstuff = $regstuff.Replace("[HKEY_CURRENT_USER\","[HKEY_USERS\$($Item.SID)\")

    # write it to our temporary location
    $regstuff | Out-File "$env:TEMP\regstuff-fixed.reg"

    write-output $($Item.Username)
    # import it
    reg.exe import "$env:TEMP\regstuff-fixed.reg"

似乎可行。

2 个答案:

答案 0 :(得分:0)

  

注意:以下是来自OP ...的答案,正在等待解答,然后再删除此发布的答案。

有点棘手的测试。

我们最终使用...

    # read in reg file with the settings we need
    $regstuff = Get-Content "\\fakecomapnyname.com\files\public\IT\Protected\Projects\SOLIDWORKS-2019-deployment\fakesettings.reg"

    # Fix it so it applies to the specific profile for this loop, profile
    $regstuff = $regstuff.Replace("[HKEY_CURRENT_USER\","[HKEY_USERS\$($Item.SID)\")

    # write it to our temporary location
    $regstuff | Out-File "$env:TEMP\regstuff-fixed.reg"

    write-output $($Item.Username)
    # import it
    reg.exe import "$env:TEMP\regstuff-fixed.reg"

似乎可行。

答案 1 :(得分:0)

我们最终使用...

# read in reg file with the settings we need
$regstuff = Get-Content "\\fakecomapnyname.com\files\public\IT\Protected\Projects\SOLIDWORKS-2019-deployment\fakesettings.reg"

# Fix it so it applies to the specific profile for this loop, profile
$regstuff = $regstuff.Replace("[HKEY_CURRENT_USER\","[HKEY_USERS\$($Item.SID)\")

# write it to our temporary location
$regstuff | Out-File "$env:TEMP\regstuff-fixed.reg"

write-output $($Item.Username)
# import it
reg.exe import "$env:TEMP\regstuff-fixed.reg"

似乎可行。