Run Registry File Remotely with PowerShell
I found the best way not to mess with issues related to server authentication and cut down on complexity just to pass Reg file as parameter to 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
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 if 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" } }