Registry value not change

283 views Asked by At

so i just start learn about powershell script my objective is to uncheck this one system properties

so i create powershell script to run the file.reg this is my test1.ps1

$username = "desktop-2ussd\viola"
$password = "qwerty"
$AdminCred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username, 
(ConvertTo-SecureString -String $password -AsPlainText -Force))
$regFile = ".\file.reg"
$regArg1 = "import $regFile"
Start-Process reg.exe -ArgumentList $regArg1 -Credential $AdminCred

and this is my file.reg

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server]
"fDenyTSConnections"=dword:00000000
"updateRDStatus"=dword:00000001

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp]
"UserAuthentication"=dword:00000000

after that i run the script like this powershell -ExecutionPolicy Bypass -File .\test1.ps1

there is no error output but the checkbox is still checked

please help me

1

There are 1 answers

3
Toni On

Currently you wont recognize if something goes wrong as you do not get a return code. In case of start-process you would need to specify the parameters:

-wait -passthru

to get the return code.

But you can directly write to the registry from PowerShell instead of using reg.exe. - e.g.:

set-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server" -name fDenyTSConnections -value 0

The above mentioned registry change gets effective immediately without restarting the related service.

Based on your comment you missed to specify the computer where the command should run. Also make use of $using to access variables of the caller machine from the remote machine e.g.:

$code = {
$newValue = $using:value
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" -Name "UserAuthentication" -Value $newValue 
}

$value = 0
invoke-command -computername [TargetComputerName] -credential $cred -scriptblock $code

In your example you did pass the value to the paramter -value as $args[0] - this only works if you specify the paramter -argumentlist of the invoke-command cmdlet. But I would advise to use $using as outlined in my example.