How to create a Windows context menu item that generates a .embyignore file with folder-specific content?
I need to create a context menu item in Windows Explorer that:
- Adds an option called “Create EmbyIgnore File” when right-clicking on a folder
- Creates a text file named “.embyignore” inside the selected folder
- Populates the file with “folder name/*” where “folder name” is the name of the folder being right-clicked
I’m familiar with creating Windows registry keys but need guidance on the specific code to dynamically insert the folder name into the file content.
To create a Windows context menu item that generates a .embyignore file with folder-specific content, you’ll need to combine registry editing with a PowerShell script that dynamically inserts the folder name. This involves creating a registry entry that points to a PowerShell script, which will then generate the .embyignore file with the appropriate “folder name/*” content.
Contents
- Registry Key Setup
- PowerShell Script Creation
- Registry Integration
- Testing and Troubleshooting
- Alternative Methods
Registry Key Setup
First, you need to create the registry structure that will add your context menu option. Open Registry Editor by pressing Win + R, typing regedit, and hitting Enter.
- Navigate to:
HKEY_CLASSES_ROOT\Directory\background\shell - Right-click on
shell→ New → Key - Name the key
CreateEmbyIgnore(or your preferred name) - Right-click on your new key → New → String Value
- Name it
Icon(optional - for custom icon) - Double-click
Iconand set value to:C:\Windows\System32\shell32.dll,3(or your preferred icon)
Now right-click on your CreateEmbyIgnore key → New → String Value → Name it Position and set it to Top to appear at the top of the context menu.
PowerShell Script Creation
Create a PowerShell script file that will generate the .embyignore file with dynamic content. Save this script as CreateEmbyIgnore.ps1 in a location like C:\Scripts\CreateEmbyIgnore.ps1:
param(
[Parameter(Mandatory=$true)]
[string]$FolderPath
)
# Get the folder name from the path
$folderName = Split-Path -Path $FolderPath -Leaf
# Define the content for the .embyignore file
$content = "$folderName/*"
# Define the full path for the .embyignore file
$ignoreFilePath = Join-Path -Path $FolderPath -ChildPath ".embyignore"
# Create the file with the dynamic content
$content | Out-File -FilePath $ignoreFilePath -Encoding UTF8
# Optional: Show success message
Write-Host "Created .embyignore file in: $ignoreFilePath"
Write-Host "Content: $content"
This script takes the folder path as a parameter, extracts the folder name, and creates the .embyignore file with the “folder name/*” content.
Registry Integration
Now create the registry entry that will execute your PowerShell script. Right-click on your CreateEmbyIgnore key → New → Key → Name it command.
Double-click on the (Default) value in the command key and set it to:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "C:\Scripts\CreateEmbyIgnore.ps1" -FolderPath "%V"
Important: Replace C:\Scripts\CreateEmbyIgnore.ps1 with the actual path to your script file. The %V variable represents the folder path when you right-click on a folder background.
For folder-specific context menus (when right-clicking on the folder itself rather than background), you might use %1 instead of %V.
Testing and Troubleshooting
To test your implementation:
- Right-click on any folder background
- You should see “Create EmbyIgnore File” in the context menu
- Click it and check if the .embyignore file is created
Common issues and solutions:
- Script not executing: Check execution policy by running
Get-ExecutionPolicyin PowerShell. If it’s Restricted, runSet-ExecutionPolicy RemoteSignedin an elevated PowerShell window. - Folder name not appearing: Ensure you’re using the correct variable (
%Vfor background,%1for folder). - File not created: Verify the script path in the registry and ensure you have write permissions to the target folder.
- Special characters: If folder names contain special characters, you may need to add additional escaping in your PowerShell script.
Here’s an enhanced version of the script that handles special characters:
param(
[Parameter(Mandatory=$true)]
[string]$FolderPath
)
try {
# Get the folder name from the path
$folderName = Split-Path -Path $FolderPath -Leaf
# Handle special characters by escaping them for the ignore file pattern
$escapedFolderName = $folderName -replace '([[\]{}()*+?.\\^$|])', '`$1'
# Define the content for the .embyignore file
$content = "$escapedFolderName/*"
# Define the full path for the .embyignore file
$ignoreFilePath = Join-Path -Path $FolderPath -ChildPath ".embyignore"
# Create the file with the dynamic content
$content | Out-File -FilePath $ignoreFilePath -Encoding UTF8
# Show success message
Write-Host "Successfully created .embyignore file in: $ignoreFilePath"
Write-Host "Content: $content"
}
catch {
Write-Host "Error creating .embyignore file: $_" -ForegroundColor Red
exit 1
}
Alternative Methods
Using Batch Script Instead of PowerShell
If you prefer a batch file approach, create CreateEmbyIgnore.bat:
@echo off
set "folderpath=%~1"
for %%F in ("%folderpath%") do set "foldername=%%~nxF"
echo %foldername%/* > "%folderpath%\.embyignore"
echo Created .embyignore file in: %folderpath%
Registry entry would be:
"%~dp0CreateEmbyIgnore.bat" "%V"
Using Registry File (.reg) for Easy Deployment
For easier deployment, create a .reg file that users can double-click:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\background\shell\CreateEmbyIgnore]
@="Create EmbyIgnore File"
"Icon"="C:\\Windows\\System32\\shell32.dll,3"
"Position"="Top"
[HKEY_CLASSES_ROOT\Directory\background\shell\CreateEmbyIgnore\command]
@="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -ExecutionPolicy Bypass -File \"C:\\Scripts\\CreateEmbyIgnore.ps1\" -FolderPath \"%V\""
Save this as AddEmbyIgnoreContextMenu.reg and users can simply double-click it to install the context menu.
Sources
- How add context menu item to Windows Explorer for folders - Stack Overflow
- Add PowerShell file (*.ps1) to New Context Menu of File Explorer - WinAero
- How to Add Custom Shortcuts to the Windows 11 or 10 Context Menu - Tom’s Hardware
- Adding right-click context menu items in Windows 10 - Klein Embedded
- Adding PowerShell command to right click menu both on and in a folder - Super User
Conclusion
Creating a Windows context menu item that generates a .embyignore file with dynamic folder-specific content is straightforward once you understand the registry-PowerShell integration. The key steps involve:
- Setting up the registry structure under
HKEY_CLASSES_ROOT\Directory\background\shell - Creating a PowerShell script that extracts the folder name and generates the appropriate content
- Integrating the script execution through the registry command key
- Testing and handling edge cases like special characters
This approach provides a seamless way to automate the creation of .embyignore files for Emby media server configuration, saving you time and ensuring consistency across your media library structure. For deployment across multiple systems, consider using the .reg file method for easy installation and removal.