Powershell adding random symbols to registry value? - powershell

I have a PowerShell script that adds a string value at a location:
Push-Location
Set-Location HKCU:
New-ItemProperty -Path
SOFTWARE\Microsoft\Office\16.0\Common\Security\Crypto\EscrowCerts -Name
"EscrowCert01" -Value '‎02 ee 7f' -PropertyType "String"
It works, the value is added. However, three symbols have been added to the start of the string.
Is there a way to stop this from happening?

Related

The requested registry access is invalid

I'm a learning System engineer and I have to Script a PowerShell Script who simply creates a Registry Key and a Value in it. I can't get it right, there is always this weird error popping up. Likewise, I already tried running it as an Administrator. Please Help!
Translation of the Error.
“The requested registry access is invalid.”
Script:
$regkey="HKEY_LOCAL_MACHINE\Software\Microsoft\Windows Defender\Reporting"
$regparam="DisableEnhancedNotifications"
cd HKLM:\
New-ItemProperty -Path "$regkey" -Name "$regparam" -Value "test" -PropertyType "String"
Thanks!
$regkey='HKLM:\SOFTWARE\Microsoft\Windows Defender\Reporting'
$regparam='DisableEnhancedNotifications'
# create key
New-Item -Path $regkey -Force
# create entry
New-ItemProperty -Path $regkey -Name $regparam -PropertyType String -Value 'test' -Force
more examples: Working with Registry Entries (learn.microsoft.com)

How do I add HKCU Regkeys value for each current user in powershell script?

I want a powershell script to run each time a user login to Windows by placed in: Shell:common startup.
This script must add about 50 Regkey's in HKCU, which is setting/path for Presetfolders for a application.
I want to use Powershell and have tried this command adding the RegKey (This command needs to be repeated for each 50 regkeys!):
New-ItemProperty -Path 'HKCU:\Software\AppName' -Name 'PresetFolder' -PropertyType String -Value '$env:userprofile\Documents\AppName\Presets1' -Force
New-ItemProperty -Path 'HKCU:\Software\AppName' -Name 'PresetFolder' -PropertyType String -Value '$env:userprofile\Documents\AppName\Presets2' -Force .......
When using "$env:userprofile" instead of c:\Users\MyUserProfile\Documents\.... the -value in the RegKey will be: "$env:userprofile\Documents\NewFolder\Presets" and not as wanted: "c:\Users\MyUserProfile\Documents\NewFolder\Presets".
I need a Variable for each userprofile!
Alternatively I can after Program installation by using admin-account, I can exported all RegKey's as a .reg-file. Before using the powershell-script to merge the RegKeys everytime a user is logging in Windows, I now need to search and replace the value of the path (-Value) from AdminUserProfil-path into a variable for each user running the script.
Part of the Reg-file:
[HKEY_CURRENT_USER\Software\AppName\Version]
"HelpDocPath"="C:\Users\\AdminUserprofiles\\Documents\\AppName\\Version\\HTML Help\\en"
"ExciterCacheInstallPath"="C:\\Program Files\\AppName\\Version\\Exciter Cache"
"DSPResourceFilesInstallPath"="C:\\Program Files\\AppName\\Version/Resources"
"InstallPath"="C:\\Program Files\\AppName\\InstallFolder"
"PresetFolder"="C:\\Users\\AdminUserprofiles\\Documents\\AppName\\Version\\Presets\\Global Presets"\
Hope anyone can help?
What do I need to type for the right path, so each user will have there own path? Do I need a variable fo rusers or..?
Thank you.
Define $env:USERPROFILE as a variable so you can call it, otherwise PS will just output what you have typed, which is what is happening in this case.
$path = "$env:USERPROFILE"
New-ItemProperty -Path 'HKCU:\Software\AppName' -Name 'PresetFolder' -PropertyType String -Value '$path\Documents\AppName\Presets1' -Force

Custom URL for toast notification buttons in registry

I trying to run a command that opens up Software Center on my machine. It works just fine if I run it in Command Prompt but not when I call it from my custom protocol handler in the registry. The main problem is that when called from the protocol handler, via a Toast button, Windows tells me I need a new app to open this.
Here is the command.
"C:\WINDOWS\CCM\ClientUX\SCClient.exe" softwarecenter:Page=Applications FilterType=0 SortType=6 View=Upcoming
Here is how I am setting up the protocol handler in registry
New-item 'HKLM:\SOFTWARE\ToastSoftwareCenter' -force
Set-itemproperty 'HKLM:\SOFTWARE\ToastSoftwareCenter' -name '(DEFAULT)' -value 'url:ToastSoftwareCenter' -force
Set-itemproperty 'HKLM:\SOFTWARE\ToastSoftwareCenter' -name 'URL Protocol' -value '' -force
New-itemproperty -path 'HKLM:\SOFTWARE\ToastSoftwareCenter' -propertytype dword -name 'EditFlags' -value 2162688
New-item 'HKLM:\SOFTWARE\ToastSoftwareCenter\Shell\Open\command' -force
Set-itemproperty 'HKLM:\SOFTWARE\ToastSoftwareCenter\Shell\Open\command' -name '(DEFAULT)' -value '"C:\WINDOWS\CCM\ClientUX\SCClient.exe" softwarecenter:Page=InstallationStatus FilterType=0 SortType=6 View=Upcoming' -force
I have tried appending these other commands on the end of the command to associate it as an exe so it wont ask anymore, but to no avail.
ftype exefile="%1" %*
assoc .exe=exefile\
The commands work when running them from a command prompt but not from the protocol handler. Am I setting it up wrong?
Thank You.
You're creating the protocol in the wrong location. It needs to be created under HKEY_CLASSES_ROOT
You also need to create the PSDrive in order to easily access that location within PowerShell.
New-PSDrive -PSProvider Registry -Root HKEY_CLASSES_ROOT -Name HKCR
New-Item 'HKCR:\ToastSoftwareCenter' -Force
Registering the Application Handling the Custom URI Scheme

Set-ItemProperty creates a duplicate registry key

I am trying to activate Long path names in Windows 10 1903.
When I run as administrator in Windows PowerShell :
Set-ItemProperty -path 'HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem' -Name 'LongPathsEnabled' -value '1'
I see that a new registry key has been created, instead of overwriting existing one.
Thanks for your help.
There is no reason to assume a bug here - your command should work as intended, but it does the wrong thing (see below).
The symptom you show - duplicate value names in a registry key - are inconsistent with the command in your question.
The underlying native registry APIs do not allow creating values with duplicate names in a given registry key; if they did - which would be a serious bug - the problem would lie there, not in PowerShell.
If the registry editor really shows two values whose names appear to be identical, the only plausible explanation is that they contain invisible characters that makes them different.
As for what you tried:
The system-known LongPathsEnabled registry value of key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem
is a 64-bit integer, of registry type REG_DWORD, equivalent to a [uint32] instance in .NET / PowerShell.
By using -Value '1' in your Set-ItemProperty call, you're letting it infer what registry type to write the value as, and that is REG_SZ, i.e. a literal string:
However, if the target value already exists, and is of - the correct - type REG_DWORD, PowerShell will convert the string for you.
By contrast, if no such value exists yet, you'll - incorrectly - create a REG_SZ value.
Your own answer now uses -Value 1, which makes the 1 implicitly an [int] (System.Int32), in which case PowerShell correctly infers REG_DWORD.
However - both for readability and robustness - consider using the -Type parameter to explicitly indicate the type of registry value you're planning to create (command spread across multiple lines for readability - note the ` chars. at the end of the interior lines, which must be the very last char. on their respective lines):
Set-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem' `
-Name 'LongPathsEnabled' `
-Value 1 `
-Type DWord
The -Type argument is of type Microsoft.Win32.RegistryValueKind, which, when you create or set a value, must be one of the following values (shown here mapped to their REG_* values):
String # REG_SZ
ExpandString # REG_EXPAND_SZ
Binary # REG_BINARY
DWord # REG_DWORD
MultiString # REG_MULTI_SZ
QWord # REG_QWORD
The full list of native REG_* types can be found here.
I figured it out.
Unquoted the value.
Set-ItemProperty -path 'HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem' -Name 'LongPathsEnabled' -value 1
Now I can play with WSL.
Enjoy.
Try this:
Push-Location
Set-Location 'HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem'
Set-ItemProperty -path .\ -Name 'LongPathsEnabled' -value 1 -Type DWord -Force
Pop-Location

Passing a password into a script

I am currently using the below PowerShell script to update an autologon value in the registry, it also checks that if the value isn't there that it is created.
Rather than opening up the script, changing the defaultpassword value in there and then running the script I'd like to be able to run the script in PowerShell and for it to prompt for the user to enter the value that is going to update.
I am new to parameters within PowerShell and can't say I'm 100% sure that is the solution.
Can anyone help? it's the 'passwordvalue' I'm looking to have inputed from a prompt and to be subsequently replaced in the script before the script runs. Let me know if I'm not being clear!
$RegKey = “HKLM:\SOFTWARE\MICROSOFT\WINDOWS NT\CURRENTVERSION\WINLOGON”
if (-Not(Test-Path “$RegKey”)) {
New-Item -Path “$($RegKey.TrimEnd($RegKey.Split(‘\’)[-1]))” -Name “$($RegKey.Split(‘\’)[-1])” -Force | Out-Null
}
Set-ItemProperty -Path “$RegKey” -Name “DefaultPassword” -Type String -Value “passwordvalue”
Just use the Read-Host cmdlet:
# ....
Set-ItemProperty -Path "$RegKey" -Name "DefaultPassword" -Type String -Value (Read-Host -Prompt 'Enter the password')