Set registry key to open notepad++ - powershell

I tried running this script using the ISE, and I also tried to run it on the command line as administrator. It freezes at the "Remove-ItemProperty" line. I've tried to remove that step, but then it freezes at the next step "Set-ItemProperty". It looks like the New-Item lines are working fine.
if (Test-path "HKCR:\")
{
}
else
{
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
}
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
Unzip "npp.6.7.5.bin.zip" “C:\Notepad++”
New-Item -Type String "HKCR:\*\shell\Open With Notepad++"
New-Item -Type String "HKCR:\*\shell\Open With Notepad++\command"
Remove-ItemProperty "HKCR:\*\shell\Open With Notepad++\command" -name "(Default)"
Set-ItemProperty "HKCR:\*\shell\Open With Notepad++\command" -name "(Default)" -value "C:\\Notepad++\\notepad++.exe %1"
Any suggestions?

When using ItemProperty commands it interprets * as a wildcard. It's not freezing, it's searching every subkey of HKCR for "shell\Open With..." etc.
To force it to interpret the whole thing as a string path you need to use the -LiteralPath switch:
New-Item -Type String "HKCR:\*\shell\Open With Notepad++"
New-Item -Type String "HKCR:\*\shell\Open With Notepad++\command"
Set-ItemProperty -LiteralPath "HKCR:\*\shell\Open With Notepad++\command" -name "(Default)" -value "C:\\Notepad++\\notepad++.exe %1"

Related

"new-item" isn't recognized as command in PowerShell

new-item -type file NAME/init.py
I have folder "NAME". The command "new-item" doesn't work. What should I do?
You should try :
New-Item -Path .\NAME -Name init.py -Type file
Use only the dot if you are located in your directory NAME, or enter the path (from the above directory or the full path).
ps: having the error output of powershell could be nice too.
This...
new-item -type file NAME/init.py
... is simply not valid syntax. See the PowerShell help files for details on the cmdlet and use. Be specific, don't guess or force PowerShell to guess for you.
For Example:
Get-ChildItem -Path 'D:/Temp' -Filter 'init.py'
# Results
<#
No results
#>
New-Item -Path 'D:\Temp' -Name 'init.py' -ItemType file -WhatIf
# Results
<#
What if: Performing the operation "Create File" on target "Destination: D:\Temp\init.py".
#>
New-Item -Path 'D:\Temp' -Name 'init.py' -ItemType file
# Results
<#
Directory: D:\Temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 18-Apr-21 20:45 0 init.py
#>
(Get-ChildItem -Path 'D:/Temp' -Filter 'init.py').FullName
# Results
<#
D:\Temp\init.py
#>
# Get specifics for a module, cmdlet, or function
(Get-Command -Name New-Item).Parameters
(Get-Command -Name New-Item).Parameters.Keys
Get-help -Name New-Item -Examples
# Results
<#
New-Item -Path .\TestFolder -ItemType Directory
New-Item -Path .\TestFolder\TestFile.txt -ItemType File
New-Item -Path .\TestFolder -ItemType Directory -Force
Get-ChildItem .\TestFolder\
New-Item ./TestFile.txt -ItemType File -Value 'This is just a test file'
#>
Get-Help -Name New-Item -Detailed
Get-help -Name New-Item -Full
Get-help -Name New-Item -Online

Powershell not creating registry key when script is run

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.

How can I create a registry value and path leading to it in one line using PowerShell?

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.

How to disable autorun for all drives

I need to run a powershell script that disables autorun for ALL drives on a computer.
It can be done manually as described here, but I need to do it for multiple computers (with Windows XP and 7) using a WDS server.
Give this a try:
$path ='HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\Explorer'
Set-ItemProperty $path -Name NoDriveTypeAutorun -Type DWord -Value 0xFF
you can try this:
function Disable-AutoRun
{
$item = Get-Item `
"REGISTRY::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\IniFileMapping\AutoRun.inf" `
-ErrorAction SilentlyContinue
if (-not $item) {
$item = New-Item "REGISTRY::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\IniFileMapping\AutoRun.inf"
}
Set-ItemProperty $item.PSPath "(default)" "#SYS:DoesNotExist"
}
and this to re-enable:
function Enable-AutoRun
{
Remove-Item "REGISTRY::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\IniFileMapping\AutoRun.inf" -Force
}
Explication.

How to turn on smart screen filter using powershell?

Can some one suggest How to turn on smart screen filter for IE using power shell script?
thanks.
Here's a link to a script to turn on Windows SmartScreen. Not sure if that's the same as IE
http://gallery.technet.microsoft.com/scriptcenter/Script-to-enable-9076d20e
This worked for me!!
Function EnableDisableSmartScreen($status) {
if($status -eq 1)
{
Write-Output "Enabling SmartScreen Filter..."
Remove-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\System" -Name "EnableSmartScreen"
Remove-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\MicrosoftEdge\PhishingFilter" -Name "EnabledV9"
}
else
{
Write-Output "Disabling SmartScreen Filter..."
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\System" -Name "EnableSmartScreen" -Type DWord -Value 0
If (!(Test-Path "HKLM:\SOFTWARE\Policies\Microsoft\MicrosoftEdge\PhishingFilter")) {
New-Item -Path "HKLM:\SOFTWARE\Policies\Microsoft\MicrosoftEdge\PhishingFilter" -Force | Out-Null
}
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\MicrosoftEdge\PhishingFilter" -Name "EnabledV9" -Type DWord -Value 0
}
}