The requested registry access is invalid - powershell

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)

Related

Create a powershell script to place a custom word template in the templates folder

I want to place a word template, template.dotm into the Word custom templates folder.
Using Office 365, latest version of Word. Windows 10. Apologies if my terminology is incorrect, still a powershell/programming novice.
This folder doesn't exist by default, and the directory Word looks for default templates in doesn't exist by default either. If a user has created a template, then it will create an expanding string named PersonalTemplates at the following registry key: HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\16.0\Word\Options, with the value being the directory they've elected as their default custom templates directory.
I want to make a script which:
Checks for presence of PersonalTemplates. If present, and value is not null, store as $regvalue.
If not present, or value is null, create expanding string with the following value $newreg at HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\16.0\Word\Options.
Then copy template.dotm into the $regvalue or $newreg. Powershell will be run from the same directory as the template.dotm is stored in.
I've got a bunch of snippets which do some of the principle operations, though I can't work out how to tie them together, and am missing some bits which I just can't work out:
Copy the template to the destination
ForEach ($user in (Get-ChildItem "C:\Users" -Exclude Public)) {
New-Item -ItemType Directory -Force -Path "C:\Users$($user.Name)\Documents\Custom Office Templates"
Copy-Item template.dotm -Destination "C:\Users$($user.Name)\Documents\Custom Office Templates"
Create registry key with value
Set-Location -Path
'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options'
New-ItemProperty -Path
'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options' -Name
'PersonalTemplates' -Value "C:\Users$($user.Name)\Documents\Custom
Office Templates" -PropertyType ExpandString -Force }
Get regvalue
$regvalue = (Get-ItemPropertyValue
'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options'
'PersonalTemplates')
I have put together your code snippets in order, also corrected the logic for checking if the Registry key is present or not.
ForEach ($user in (Get-ChildItem "C:\Users" -Exclude Public))
{
$location = "C:\Users\$($user.Name)\Documents\Custom Office Templates"
$IsPresent = Get-ItemProperty 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options' | ForEach-Object {If($_ -like '*PersonalTemplates*'){ Return 'True' }}
if(-Not($IsPresent -eq 'True'))
{
New-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options' -Name 'PersonalTemplates' -Value $location -PropertyType ExpandString -Force \\Not tested
New-Item -ItemType Directory -Force -Path $location
}
$existingValue= Get-ItemPropertyValue -Path 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options' -Name 'PersonalTemplates'
if([string]::IsNullOrWhiteSpace($existingValue)){
Set-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options' -Name 'PersonalTemplates' -Value $location
}
else{
$location=$existingValue
if(!(test-path $existingValue))
{
New-Item -ItemType Directory -Force -Path $existingValue
}
}
Copy-Item template.dotm -Destination $location
}
I have not tested the creation of registry key as I am on my work laptop, so assuming that line of code works.
Also, question for you: With this approach wouldn't the registry entry have the single value that of the first user folder, you may have to look into the logic? I feel you may have to run this script for each user after they login using $env:Username instead of looping through the user folder. But I could be wrong, there may be other who could suggest better.

Add trusted File Share to internet explorer

I need a bit of help, I need to add my File Share \169.254.100.100\Share to the trusted Sites in IE, I did it the manual way, so I know that is working. But I'd like to do it also in PS. I tried adding it in the Registry and GPO in different ways but failed. For websites it works but not for the Server.. any suggestions?
New-Item -Path '.\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap' -Name \\169.254.100.100\Share
New-ItemProperty -Path '.\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains' -Name\\169.254.100.100\Share -Value 1 -PropertyType Dword
But this fails which is ok but I don't have other ideas.
BR Tim
If you're adding an IP Address it goes in the Ranges subkey not the Domains subkey, you also need to specify the file URL scheme and populate the ZoneMapKey. The parent subkeys may not exist, on my test PC even ZoneMap was not present. The example below should be expanded to test what exists and what needs creating.
Set-Location "HKCU:"
New-Item -Path '\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\' -name ZoneMap
New-Item -Path '\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap' -name Domains
New-Item -Path '\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap' -name Ranges
New-Item -Path '\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Ranges' -name Range1
New-ItemProperty -Path '\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Ranges\Range1' -Name ":Range" -Value "169.254.100.100"
New-ItemProperty -Path '\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Ranges\Range1' -Name "file" -Value 2
New-Item -Path '\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\' -name ZoneMapKey
New-ItemProperty -Path '\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMapKey' -Name "file://169.254.100.100" -Value "2"
You'd need to make sure Range1 doesn't exist and find RangeN where N doesn't exist.

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

Powershell adding random symbols to registry value?

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?