Associating .VBS and .JS File Types to Open in Notepad by Default

New-PSDrive HKU Registry HKEY_USERS > $NULL
$username = Gwmi -Class Win32_ComputerSystem | select username
$objuser = New-Object System.Security.Principal.NTAccount($username.username)
$sid = $objuser.Translate([System.Security.Principal.SecurityIdentifier])
$SIDValue = $sid.Value

Set-ItemProperty -Path HKLM:Software\Classes\jsfile\DefaultIcon -Name ‘(Default)’ -Value “C:\Windows\system32\NOTEPAD.EXE,0” -Force
Set-ItemProperty -Path HKU:$SIDValue\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts.js -Name ‘(Default)’ -Value ‘notepad.exe’ -Force
Set-ItemProperty -Path HKU:$SIDValue\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts.js\OpenWithList -Name ‘(Default)’ -Value ‘notepad.exe’ -Force

cmd /c assoc .js=jsfile
cmd /c ftype jsfile=%SystemRoot%\system32\NOTEPAD.EXE %1

Set-ItemProperty -Path HKLM:Software\Classes\vbsfile\DefaultIcon -Name ‘(Default)’ -Value “C:\Windows\system32\NOTEPAD.EXE,0” -Force
Set-ItemProperty -Path HKU:$SIDValue\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts.vbs\ -Name ‘(Default)’ -Value ‘notepad.exe’ -Force
Set-ItemProperty -Path HKU:$SIDValue\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts.vbs\OpenWithList -Name ‘(Default)’ -Value ‘notepad.exe’ -Force

cmd /c assoc .vbs=vbsfile
cmd /c ftype vbsfile=%SystemRoot%\system32\NOTEPAD.EXE %1

This PS script is what was provided to our team to associate both .vbs and .js file types to open in Notepad by default. This is a requirement we have from security, but want to allow the users to switch it to another application if they so choose. Not many users use these file types so this is more of a precaution so the typical user doesn’t run a .vbs or .js file not knowing what it will execute. When running the script above, it is not associating the file types when I create a .js and .vbs file to Notepad as I would expect. It makes no changes but it is making the registry changes. I have tried it with 3 other users as well. One of them it worked for the .vbs file but not the .js file type which is what it is doing for me. The other 2 users it worked. I have tried running this through ChatGPT and Bing’s CoPilot but not getting anywhere. Even have tried codes generated out of both and still isn’t working. We will be deploying this all out all laptops in our environment through SCCM but testing locally first. I am new to powershell myself and have been on this for about a week trying to resolve, but myself and team have not been able to resolve as of yet. Any help would be appreciated. If you see something wrong with the code or think we should go about it with a different approach, that is fine as well. I am open to anyway to get this resolved.

If this is not allowed or if I have done something wrong by posting this, please let me know or delete this post. This is my first post on these forums.

Wow … this is going way back in time. I did something similar to this a while back but using GPO registry settings. I do believe you need to modify the HKEY_CLASSES_ROOT hive entries to get this done. You can google to get whats needed.

[HKEY_CLASSES_ROOT\VBSFile\Shell]
[HKEY_CLASSES_ROOT\VBSFile\Shell\Open\Command]
[HKEY_CLASSES_ROOT\VBSFile\Shell\Open2]
[HKEY_CLASSES_ROOT\VBSFile\Shell\Open2\Command]

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.