Run the registry file remotely using PowerShell
I am using the following script to run test.reg
on multiple remote systems:
$computers = Get-Content computers.txt
Invoke-Command -ComputerName $computers -ScriptBlock {
regedit /i /s "\\SERVER\C$\RegistryFiles\test.reg"
}
The script is not an error, but the registry entry is not being imported to either system.
I know the file test.reg
is a valid registry file because I copied it over, ran it manually, and imported the registry key. I also made sure PowerShell Remoting is enabled on remote computers.
Any ideas why the registry key is not being imported?
source to share
I found a better way not to get confused with server authentication issues and reduce the complexity just to pass the Reg file as a parameter to the function.
$regFile = @"
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters]
"MaxUserPort"=dword:00005000
"TcpTimedWaitDelay"=dword:0000001e
"@
Invoke-Command -ComputerName computerName -ScriptBlock {param($regFile) $regFile | out-file $env:temp\a.reg;
reg.exe import $env:temp\a.reg } -ArgumentList $regFile
source to share
I posted on some PowerShell forums and finally got this working.
I had to 1) move the $ newfile variable inside the loop and 2) comment out the $ in the path stored in the $ newfile variable.
For reference, the final script looks like this in case anyone wants to use it:
$servers = Get-Content servers.txt
$HostedRegFile = "C:\Scripts\RegistryFiles\test.reg"
foreach ($server in $servers)
{
$newfile = "\\$server\c`$\Downloads\RegistryFiles\test.reg"
New-Item -ErrorAction SilentlyContinue -ItemType directory -Path \\$server\C$\Downloads\RegistryFiles
Copy-Item $HostedRegFile -Destination $newfile
Invoke-Command -ComputerName $server -ScriptBlock {
Start-Process -filepath "C:\windows\regedit.exe" -argumentlist "/s C:\Downloads\RegistryFiles\test.reg"
}
}
source to share