2

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?

BigRedEO
  • 807
  • 4
  • 13
  • 33

4 Answers4

6

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
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
  • 3
    It might be better to `Test-Path -Path "HKLM:\System\CurrentControlSet\Control\Terminal Server"` first, and only do the `New-Item` if the `Test-Path` fails - otherwise, you're 'blowing away' all of the TS settings. – Jeff Zeitlin Oct 02 '19 at 17:24
  • @JeffZeitlin really? in the filesystem provider, I noticed it's not destructive for existing items. Wasn't aware that isn't the case with the registry provider. – Maximilian Burszley Oct 02 '19 at 17:25
  • I've seen inconsistent behavior in the registry; even where I "know" that it doesn't affect extant items in a provider, I consider it 'best practice' to test-and-create rather than just create-forcibly. – Jeff Zeitlin Oct 02 '19 at 18:02
  • Thanks! This exactly help to solve my problem. – MaxiPalle Nov 30 '20 at 11:49
  • @JeffZeitlin What if overwriting (in case a key exists) is the goal? Would deleting it first be safe? Or is there an overwrite flag? – gargoylebident Jun 12 '21 at 17:23
  • @stackexchange_account1111 - Delete-then-recreate would generally be safe for _properties_ (and is arguably a 'best practice'). For _keys_ (represented as folders in RegEdit), you'd have to consider whether you need to preserve extant properties. – Jeff Zeitlin Jun 13 '21 at 01:23
1

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
Mike
  • 131
  • 1
  • 7
0

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.

BigRedEO
  • 807
  • 4
  • 13
  • 33
0

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

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – MD. RAKIB HASAN Dec 14 '21 at 08:37