I'm trying to find a way to access the 'New File' commands that pop up in the right-click menu in Windows Explorer directly through PowerShell. This menu lets you create different types of files depending on the applications installed on your system. I've been experimenting with some commands, like using the Shell.Application COM object, but I haven't had any luck so far. For instance, this code snippet doesn't work as expected:
$shell = New-Object -ComObject "Shell.Application"
$folder = $shell.Namespace("C:temp")
$folder.ParseName(".").InvokeVerb("New")
I can list installed verbs with a command based on ProcessStartInfo, but accessing the 'New File' functionality seems tricky. I'm particularly interested in creating binary files, not just plain text files. Any ideas on how to make this work? I'm currently using PowerShell 7.4.
5 Answers
So here's the deal: the context menus are managed through the registry. If you're looking for those 'New' file options, you'd want to check the registry keys related to specific file types. For some file types, you may just create a file with the right extension, while for others, there's a specific process you need to follow. The registry is where all this setup lives, so digging through there will help you understand how to create those files.
You can't directly invoke the 'New' menu in PowerShell as there isn't an API for it, but you can achieve similar results with some registry magic. For example, if you want to create an Access database, you’d need to get the default value for the extension, read the ShellNew registry entries, and then copy those files to your desired location. Keep in mind this method could fail if the required application isn't installed, so it's good to know what you’re aiming for first!
The 'New' menu is indeed driven by registry entries. You would need to go through the registered file types, look for ones that support creating new files, and then invoke those commands. It's not quite as simple as using a command that's already built-in; you'll be piecing it together yourself.
You can also use Get-Item against the ShellNew path in the registry to pull all the available new items. If they have a NullFile property, creating an empty file with New-Item is straightforward. Otherwise, check the command linked to that item.
If you're specifically interested in zip files, there's a way to create a new empty zip file programmatically too. Just write the header bytes to create a new file, if that's what you need! There's also related registry entries where you could find more details about handling zip files if you're curious about that.
Yeah, I think the original poster is after the ease of use that comes with the context menu, wanting to avoid duplicating work. It’d definitely be more efficient to leverage what’s already built into the OS if possible.