When I try to set a registry key from a powershell script it overwrites another key :
For example :
$registryKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Qualys\QualysAgent\ScanOnDemand\Vulnerability"
$valuedata = '1'
$valuename = "Scanondemand"
Set-ItemProperty -Path $registryKey -Name $ValueName -Value $ValueData
This sets registry key right. Then I change the valuename:
$valuename = 'ScanOnstartup'
Set-ItemProperty -Path $registryKey -Name $ValueName -Value $ValueData
On now the Scanonstartup is correct but the Scanondemand key is gone. It kind of renames the name instead of creating a new key.
You may be looking to replace your second add with:
New-ItemProperty
The problem is that you do not specify the PowerShell drive in the registry path.
You can either use the long form:
Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Qualys\QualysAgent\ScanOnDemand\Vulnerability
or make use of the fact that for HKEY_LOCAL_MACHINE PowerShell already has a drive set up by name HKLM:
HKLM:\SOFTWARE\Qualys\QualysAgent\ScanOnDemand\Vulnerability
Your code would then be
$registryKey = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Qualys\QualysAgent\ScanOnDemand\Vulnerability'
# or: $registryKey = 'HKLM:\SOFTWARE\Qualys\QualysAgent\ScanOnDemand\Vulnerability'
$valuedata = '1'
$valuename = 'ScanOnDemand', 'ScanOnStartup'
# if the key does not already exist, create it first
if (!(Test-Path -Path $registryKey)) {
$null = New-Item -Path $registryKey
}
Set-ItemProperty -Path $registryKey -Name $ValueName[0] -Value $ValueData
Set-ItemProperty -Path $registryKey -Name $ValueName[1] -Value $ValueData
By using switch -Force to the New-Item cmdlet, you do not have to check if that key already exists because it will return either the existing key as object or the newly created one. Because we have no further use for that object as such, we ignore it with $null = New-Item ..
After running this (as Administrator), you have created this keys structure in the registry:
And in subkey Vulnerability you now have these two entries:
Related
I am trying to get a registry key value , ExtensionSettings, if there is something in the registry then save it. Then add a new entry to it . The two entries have to have a comma in-between values. I can not figure the code out . I am not sure what I am doing wrong or missing. Thank you so much for your help.
# Set variables to indicate value and key to set
$RegistryPath = 'HKLM:SOFTWARE\Policies\Google\Chrome'
$Name = 'ExtensionSettings'
$Value = '{"feolagkacappiaieohahjkeaikhjjcfa":{"toolbar_pin":"force_pinned"}}'
# Create the key if it does not exist
If (-NOT (Test-Path $RegistryPath)) {
New-Item -Path $RegistryPath -Force | Out-Null
}
#$OldValue = Get-ItemProperty -Path $RegistryPath -Name $Name
#$oldValue = $OldValue.ExtensionSettings
$OldValue = (Get-ItemProperty "HKLM:SOFTWARE\Policies\Google\Chrome").ExtensionSettings
#$NewValue = New-ItemProperty -Path $RegistryPath -Name $Name -Value $value
$UpdatedValue = $OldValue + "," + $NewValue
$NewValue = $NewValue.ExtensionSettings
New-ItemProperty -Path $RegistryPath -Name $Name -Value $UpdatedValue -PropertyType String -Force
Here is the error I get. I have updated the code with what I have tried . Nothing is written to the registry key. It goes show the comma but not the rest.
error I receive is
New-ItemProperty : The property already exists.
At line:15 char:13
+ $NewValue = New-ItemProperty -Path $RegistryPath -Name $Name -Value $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceExists: (HKEY_LOCAL_MACH...s\Google\Chrome:String) [New-ItemProperty], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.NewItemPropertyCommand
The "Extension Settings" registry key is a JSON-formatted string, so you won't be able to simply concatenate on to the end of it. Here's a sample string that Google provides as an example . . . notice that the entire string is encapsulated in curly braces. In order to add an entry, you'd have to add it inside the outer set of curly braces.
{
"*": {
"installation_mode": "blocked"
},
"nckgahadagoaajjgafhacjanaoiihapd": {
"installation_mode": "force_installed",
"update_url":
"https://clients2.google.com/service/update2/crx"
},
"lpcaedmchfhocbbapmcbpinfpgnhiddi": {
"installation_mode": "normal_installed",
"update_url":
"https://clients2.google.com/service/update2/crx"
},
"gmbgaklkmjakoegficnlkhebmhkjfich": {
"installation_mode": "allowed"
}
}
Something like the following should work (note that the original $Value has been modified to remove the outer curly braces):
# Set variables to indicate value and key to set
$RegistryPath = 'HKLM:SOFTWARE\Policies\Google\Chrome'
$Key = 'ExtensionSettings'
$ValueAdd = '"feolagkacappiaieohahjkeaikhjjcfa":{"toolbar_pin":"force_pinned"}'
# Check for existing 'ExtensionSettings' key and modify/create
If ((Get-Item -Path $RegistryPath).GetValue($Key) -ne $null) {
# Copy existing value (without outer braces)
$string = (Get-ItemProperty "$RegistryPath").$Name
$ValueOld = $string.subString(1, ($string.Length)-2)
# Modify to add new value (and add outer braces)
$ValueNew = "{$ValueOld, $ValueAdd}"
New-ItemProperty -Path $RegistryPath -Name $Key -Value $ValueNew -PropertyType String -Force
} else {
# If it doesn't already exist, create it
$ValueNew = "{$ValueAdd}"
New-ItemProperty -Path $RegistryPath -Name $Key -Value $ValueNew -PropertyType String -Force
}
My code:
try {
if(!(Test-Path -Path $registryPath -Value $Name)) {
New-ItemProperty -Name test -Path HKLM:\Software\WOW6432Node\Mozilla -PropertyType DWord -Value 2
}
}
catch {
Set-ItemProperty -Path $registryPath -Name $Name -Value $value
}
My Problem: the result comes out as string.
I have tried changing -PropertyType to -Type.
I have tried changing the capitilation of the word DWord.
Any help would be greatly appreciated.
As the docs say:
You also use Set-ItemProperty to create and change registry values and data. For example, you can add a new registry entry to a key and establish or change its value.
With Set-ItemProperty it is also possible to change the type of registry entry from say a REG_SZ (string) value to DWord, so basically all you need is:
$registryPath = 'HKLM:\Software\WOW6432Node\Mozilla'
$propName = 'test'
Set-ItemProperty -Path $registryPath -Name $propName -Value 2 -Type DWord
Of course, if you do not have permissions to create or alter something in the HKEY_LOCAL_MACHINE registry path, you will rceive an error.
We are running Coded UI tests on IE in Windows 8.1, and we're doing it through Visual Studio Team Services. As part of our build, we run a Powershell script that disables the popup manager. The code we use to disable it is this:
Remove-ItemProperty "HKCU:\Software\Microsoft\Internet Explorer\New Windows" -Name "PopupMgr"
New-ItemProperty "HKCU:\Software\Microsoft\Internet Explorer\New Windows" -Name "PopupMgr" -Value 00000000 -PropertyType "DWord"
When I create and deploy a build in Release Manager, running this generates the following error:
The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: Cannot find path 'HKCU:\Software\Microsoft\Internet Explorer\New Windows' because it does not exist.
(Emphasis mine)
I've logged onto the VM and looked at the registry, and HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\New Windows absolutely exists. The only thing I can think of is that its value isn't a DWORD but rather a string -- the PopupMgr key has a Data value of "yes" rather than 1 or 0. But that doesn't match the error message -- the error says it can't even find the path of the key, not that the value type is a mismatch. Plus, the code removes the existing key before inserting a new one, so I don't even know how it would notice the mismatch.
Even weirder, if I open Powershell inside the VM and run those exact two lines (I copied and pasted to avoid typos), they ran just fine.
This script worked on Windows 10 perfectly and has for a while, so I'm unsure what's going on here. The user is a member of the Administrators group, so I don't think it's a permissions issue.
Can anyone shed some light on this?
I think you are trying to add the Registry key Property Value
You need to test for the existence of the registry key. If the registry key does not exist, then you need to create the registry key, and then create the registry key property value.
You should create the path to the registry key, then specify the property name and the value you want to assign. This consists of three variables as shown here:
This should help you out:
$registryPath = "HKCU:\Software\Microsoft\Internet Explorer\New Windows"
$Name = "PopupMgr"
$value = "00000000"
IF(!(Test-Path $registryPath))
{
New-Item -Path $registryPath -Force | Out-Null
New-ItemProperty -Path $registryPath -Name $name -Value $value `
-PropertyType DWORD -Force | Out-Null}
ELSE {
New-ItemProperty -Path $registryPath -Name $name -Value $value `
-PropertyType DWORD -Force | Out-Null}
Hope it helps.
I had the same problem when renaming values, so I understood that for values you can (need / should) just define them, so I stopped trying to rename these values and started to define them :)
Just try to use Set-ItemProperty to change, set and reset values ...
Set-ItemProperty -Path "HKCU:\Software\Microsoft\Internet Explorer\New Windows" -Name PopupMgr -Value 00000000 -Type DWORD
Some further reading:
[√] MS-Doc's | set-itemproperty
[√] SS64.com | set-itemproperty
I wrapped Duttas answer in an easy to use function:
Set_Registry_key -key "HKLM:\FULL\PATH\pad\pat\KEYNAME" `
-type String `
-value 'value'
Function definition:
function Set_Registry_key{
# String REG_SZ; ExpandString: REG_EXPAND_SZ; Binary: REG_BINARY; DWord: REG_DWORD; MultiString: REG_MULTI_SZ; Qword: REG_QWORD; Unknown: REG_RESOURCE_LIST
Param(
[Parameter(Mandatory=$true)]
[string]
$key,
[ValidateSet('String', 'DWord', 'ExpandString', 'Binary', 'MultiString', 'Qword', 'Unknown')]
$type,
[Parameter(Mandatory=$true)]
$value
)
$registryPath = $key -replace "[^\\]*$", ""
$name = $key -replace ".*\\", ""
if(!(Test-Path $registryPath)){
New-Item -Path $registryPath -Force | Out-Null
New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType $type -Force | Out-Null
}else {
New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType $type -Force | Out-Null
}
}
I am currently creating a powershell script in which the technicians can run to help apply various registry edits to create certain PCs which have automatic logins. However, whenever I run my script the powershell has no issue when changing values with pre-existing keys, yet it will not create keys when using the "new-item" command. I was wondering whether anyone would have any idea as to why this would not create the registry key given that I receive no errors when run.
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" –Name AutoAdminLogon -Value 1
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultUserName -Value domain\TEST
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultPassword -Value TEST123
Test-Path –Path "HKLM:\SOFTWARE\Microsoft\WindowsNT\CurrentVersion\Winlogon\ForceAutoLogon"
if ( -Not (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"))
{
New-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name ForceAutoLogon -Value 1
}
Test-Path is not designed for registy values. What you can do it use a Try/Catch block. You also need to Get/Set the itemPropery.
$Path = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
Try {
Get-ItemProperty -Path $Path | Select-Object -ExpandProperty ForceAutoLogon -ErrorAction Stop | Out-Null
Set-ItemProperty -Path $Path -Name ForceAutoLogon -Value 1
} Catch {
New-ItemProperty -Path $Path -Name ForceAutoLogon -Value 1
}
If the Get-ItemProperty fails the the key must not exist. Then we can create it! If Get-ItemProperty succeeds then we can ensure the value is set properly. I might be using the registry keywords wrong but let the code speak for itself.
Creating a registry value, including the path up to it, and not erroring if the path already exists is easy using old-school reg.exe:
reg add HKCU\Software\Policies\Microsoft\Windows\EdgeUI /f /v DisableHelpSticker /t reg_sz /d 1
That's nice and concise. The shortest way I found to do it in pure PowerShell is two lines, or three if you don't want to repeat the path:
$regPath = 'HKCU:\Software\Policies\Microsoft\Windows\EdgeUI'
New-Item $regPath -Force | Out-Null
New-ItemProperty $regPath -Name DisableHelpSticker -Value 1 -Force | Out-Null
Is there an easier way using pure PowerShell? And without adding a utility function.
You can pipe the creation line to the New-ItemProperty line as follows, but be aware that the -Force flag on New-Item will delete any pre-existing contents of the key:
New-Item 'HKCU:\Software\Policies\Microsoft\Windows\EdgeUI' -Force | New-ItemProperty -Name DisableHelpSticker -Value 1 -Force | Out-Null
Another way of simplifying PS registry handling is calling the .Net function SetValue() directly:
[microsoft.win32.registry]::SetValue("HKEY_CURRENT_USER\Software\Test", "Test-String", "Testing")
[microsoft.win32.registry]::SetValue("HKEY_CURRENT_USER\Software\Test", "Test-DW", 0xff)
Why not just use the following:
New-ItemProperty -Path Registry::HKLM\SOFTWARE\Policies\Microsoft\Windows\EdgeUI -Name DisableHelpSticker -Value Contoso -Force | Out-Null
I am using this for some SCCM stuff and it doesn't overwrite my existing reg entries.
I didn't have any luck with #mschomm's answer for setting a DWORD value. Finally got this code to work for me:
$RegName = '2233969290'
$RegPath = 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Policies\Microsoft\FeatureManagement\Overrides\'
$RegData = 0
Write-Output "Setting Registry Key"
[microsoft.win32.registry]::SetValue($RegPath, $RegName, $RegData, [Microsoft.Win32.RegistryValueKind]::DWORD)
Here is another one-liner:
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\NamingTemplates' | % { if (Test-Path $_) { Get-Item $_ } else { New-Item $_ } } | Set-ItemProperty -Name 'RenameNameTemplate' -Value (Get-Date -Format 'yyyy-MM-dd')
This sample sets the default name of a new explorer folder to the current date. I run this one in a scheduled task at 00:00h every day.