BigRedEO
BigRedEO

Reputation: 847

Powershell to add reg key

I have a Powershell script that "builds" a PC from the basic Windows operating system on up (Windows 7 Pro - will be converted to 10 next year). I have a number of reg keys that get added when running this script and they all work fine, no problems.

I am having to add a new reg key which turns off Remote Desktop Services. I can do it at the cmd line with

reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 1 /f

which works fine and dandy. So now I need to add this same key via a Powershell script and I can't get it to work. What I have is

New-Item -Path 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server' -Name fDenyTSConnections -Value 1 | Out-File $log -append

and when I run that, something pops up that reads

Type:

So I assumed it is asking for a type. But if I add PropertyType as below

New-Item -Path 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server' -Name fDenyTSConnections -PropertyType DWORD -Value 1 | Out-File $log -append

it gives an error. I've researched at several forums online and nothing seems to work. Any ideas?

Upvotes: 4

Views: 75705

Answers (4)

Ventero
Ventero

Reputation: 1

Error is probably in first command syntax "New-Item -Path" Correct is "New-ItemProperty -Path" https://learn.microsoft.com/en-us/powershell/scripting/samples/working-with-registry-entries?view=powershell-7.2#creating-new-registry-entries

Upvotes: 0

Mike
Mike

Reputation: 131

I always create Registry Keys/Values like this:

# Set the location to the registry
Set-Location -Path 'HKLM:\Software\Microsoft'

# Create a new Key
Get-Item -Path 'HKLM:\Software\Microsoft' | New-Item -Name 'W10MigInfo\Diskspace Info' -Force

# Create new items with values
New-ItemProperty -Path 'HKLM:\Software\Microsoft\W10MigInfo\Diskspace Info' -Name 'usedDiskspaceCDrive' -Value "$usedDiskspaceCDrive" -PropertyType String -Force
New-ItemProperty -Path 'HKLM:\Software\Microsoft\W10MigInfo\Diskspace Info' -Name 'usedDiskSpaceDDrive' -Value "$usedDiskspaceDDrive" -PropertyType String -Force

# Get out of the Registry
Pop-Location

Upvotes: 1

Maximilian Burszley
Maximilian Burszley

Reputation: 19684

You cannot create a registry key with properties at the same time. You need to do one, then the other:

$path = 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server'

$key = try {
    Get-Item -Path $path -ErrorAction Stop
}
catch {
    New-Item -Path $path -Force
}

New-ItemProperty -Path $key.PSPath -Name fDenyTSConnections -Value 1

Upvotes: 9

BigRedEO
BigRedEO

Reputation: 847

What I finally tested and got working was:

cmd /c 'reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 1 /f' | Out-File $log -append

I realize that's going around the Powershell option, but it's what I got to work for now. Curious to see what I'm missing in the Powershell command, though.

Upvotes: 0

Related Questions