3

I've written this PowerShell script to automatically block executables inside a folder in Windows Firewall

$currentdir = get-Location
cd $currentdir



function FirewallBlock { 
    param ($exes)
    
    
           $exes | ForEach-Object { New-NetFirewallRule -DisplayName "$_.name inbound" -Direction Inbound -Action Block -Profile Any -Program $_.DirectoryName }
           $exes | ForEach-Object { New-NetFirewallRule -DisplayName "$_.name outbound" -Direction Outbound -Action Block -Profile Any -Program $_.DirectoryName }
                 
}


$programs = dir -Recurse *.exe
FirewallBlock ($programs)

Added it to the old style context menu by creating 2 registry keys here:

Computer\HKEY_CLASSES_ROOT\Directory\Background\shell\Firewall

and here

Computer\HKEY_CLASSES_ROOT\Directory\Background\shell\Firewall\command

Firewall key has the default name and value of "Firewall Block Programs"

command key has the default name and string value of

powershell -command "Start-Process pwsh.exe -ArgumentList '-file C:\Users\UserName\OneDrive\Desktop\Firewall.ps1' -Verb runAs"

How can I add this context menu option to the new modern context menu style in Windows 11?

so far I found this but couldn't figure out if I really have to create a program out of this simple PowerShell script in order to add it there or not.

https://blogs.windows.com/windowsdeveloper/2021/07/19/extending-the-context-menu-and-share-dialog-in-windows-11/

  • You need to implement `IExplorerCommand` in a DLL. From a quick search I found [this example](https://github.com/microsoft/AppModelSamples/blob/master/Samples/SparsePackages/PhotoStoreContextMenu/dllmain.cpp) in C++. – zett42 Nov 03 '22 at 12:24
  • @zett42 Thank you, I'm not familiar with C++, could you explain more and possibly post a complete answer? it will help a lot of people since it's a new topic :) –  Nov 03 '22 at 18:41
  • I would have to research this on my own first, which I'm currently not interested in. Maybe there is a free tool to add custom entries to the new context menu. – zett42 Nov 03 '22 at 18:45

1 Answers1

0

I've been looking for the same thing for quite a while, and finally found the best way to do it!

You can actually do this without making an entire application for it. You can use the Custom Context Menu app from Microsoft Store, it lets you add anything you want to the new Windows 11 context menu.

I've been looking for this every so often for like half a year, feels good now that I've found a working solution.

Ivan Ermakov
  • 11
  • 1
  • 5