Registry Key is not getting created inside HKCR using powershell script - powershell

I'm new to this and want to create a registry inside HKCR using powershell, but its not creating when i tries to run complete script at once.
However it gets created when i select that part and run seperately.
Am i missing something
Here is my code for creating a registry:
if (Get-PSDrive HKCR -ErrorAction SilentlyContinue) {
#Write-Host 'The X: drive is already in use.'
} else {
New-PSDrive -PSProvider Registry -Root HKEY_CLASSES_ROOT -Name HKCR
}
$path4="HKCR:\CLSID\"
New-Item -Path $path4 -Name "<key Name>" -Force
$path5=$path4+"<key Name>"
New-ItemProperty $path5 -Name ‘(Default)’ -Value "PanPlapProvider" -Force
Any help will be thankfull.

HKCR hive does not exist actually. This virtual registry hive is combined of HKEY_LOCAL_MACHINE\SOFTWARE\Classes and HKEY_CURRENT_USER\Software\Classes.
Doc says:
HKEY_CLASSES_ROOT key provides a view of the registry that merges the
information from HKEY_LOCAL_MACHINE\Software\Classes with the
information from HKEY_CURRENT_USER\Software\Classes.
...
To change the settings for the interactive user, store the changes
under HKEY_CURRENT_USER\Software\Classes rather than
HKEY_CLASSES_ROOT.
etc, read this doc carefully.

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)

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

Updating registry remotely in powershell not saving after reboot

I am doing an automated deployment process within a single server that deploys a few different virtual machines. Once all the virtual machines are imported using a base image I start them and rename them and try to setup the auto logon process by setting the expected registry keys.
Remove-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "AutoLogonCount" -Force -ErrorAction SilentlyContinue
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "DefaultUserName" -Value "$Username" -Force
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "DefaultPassword" -Value "$Password" -Force
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "AutoAdminLogon" -Value "1" -Force
This is wrapped in an Invoke-Command with a working session and passing in the correct information into it. After this has been ran I use [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey( method to obtain the key value information and verify it has been set correctly. Which everything equals what it should.
However once this is complete I do a reboot of the virtual machine but occasionally a vm will not have the registry keys saved and it will not login automatically. When I manually login and look at the registry the keys are blank or not created.
I even have it written up that after the reboot if the registry keys do not equal what they should to try again, and after every attempted reboot to login automatically the keys seemingly disappear. I am running it again as I am writing this to attempt to do it and this time look in the event logs to see if anything is found.
This process can take some time since it doesn't seem to happen consistently and I delete the vms and run the automated process from scratch.

Configuring HKEY_CURRENT_USER for all Users with DSC

I'd like to overwrite an existing registry key in HKCU for all users on this device with Desired State Configuration.
I already found, that I could use the users credentials(Configuring HKEY_CURRENT_USER with DSC Resource actually updates HKEY_USERS\.DEFAULT), but I do not have them.
Any ideas?
Technically if your administrator on the Box you can still do this,
Load their Registery Hive and Change a Key.
$Username = "TestUser"
reg load hku\$Username C:\Users\$Username\NTUSER.DAT
New-PSDrive -Name $Username -PSProvider Registry -Root HKU\$Username
Set-Location "$($Username):\Control Panel\desktop\colors"
Get-ItemProperty -Path. -Name ActiveBorder
Set-ItemProperty -Path. -Name ActiveBorder -Value "218 208 200"
Get-ItemProperty -Path. -Name ActiveBorder
Remove-PSDrive -Name $Username -Force

PowerShell map persistent sharepoint path

I'm trying to map the path to a SharePoint document library in a persistent way. It's strange that this works fine:
$Sharepoint = '\\domain.net\stuff\Documents\Folders - Permission matrix'
New-PSDrive -Name P -Root $Sharepoint -PSProvider FileSystem -Credential $Credentials
But this doesn't work:
New-PSDrive -Persist -Name P -Root $Sharepoint -PSProvider FileSystem -Credential $Credentials
New-PSDrive : The network resource type is not correct
At line:1 char:1
+ New-PSDrive -Persist -Name P -Root $Sharepoint -PSProvider FileSystem -Credentia ...
The commands are both using the same PSProvider but one is persistent and the other one not. How can I have this persistent without reverting to net use?
I ran into this problem a few weeks back in a script which mysteriously stopped worked whilst I was developing it, seems to be a Windows error 66 rather than Powershell as explained here
Here is an alternative to net use which uses credentials
# map drive using credentials
(New-Object -ComObject WScript.Network).MapNetworkDrive("$LocalDrive","\\$computer\$drive",$false,$($credentials.username),$($credentials.GetNetworkCredential().password))
I tend to use PSDrive like this
# discover and delete
if (Get-PSDrive -Name Results -ErrorAction SilentlyContinue) { Remove-PSDrive -Name Results -Scope Global | Out-Null }
# create using credentials
if ((New-PSDrive -Name Results -PSProvider FileSystem -Root "\\server\share" -Credential $Credentials -Scope Global -ErrorAction SilentlyContinue) -eq $null) { $WSHShell.popup(“You do not have access to the results repository“,0,””,0) | Out-Null }
# call from a separate function
(Get-PSDrive -Name Results).root
It might just be that a reboot will solve the problem because I cannot recreate the issue today.
I had a similar issue, turns out Windows 2016 and 2019 needs WEBDav Redirection installed.
I was getting error 53, when trying to map a SharePoint library.