How to run a poweshell script from the context menu (on audio file)?

I did try this test.reg

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\audio\shell\ChunkAudio]@="chunk audio (5 min)""Icon"="%SystemRoot%\\System32\\shell32.dll,186"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\audio\shell\ChunkAudio\Command]@="%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe C:\Users\Me\Desktop\5min_chunk_audio.ps1 %1"

It adds a context menu on the audio files as expected, but it sends an error when I click on it:

Windows не может получить доступ к указанному устройству, пути или файлу. У вас могут не быть соответствующих разрешений для доступа к элементу.

It might be because the test.reg doesn't add anything to the field "Data" in....shell\ChunkAudio\Command in the registry. (I tried to manualy add the %SystemRoot%\system32\... C:\...audio.ps1 %1)

Я также попробовал:

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\audio\shell\ChunkAudio\Command]
@="CMD.EXE /C Powershell.exe -File C:\\Users\\Me\Desktop\\petit_program\\PowerShell\\PowerShell_script\\5min_chunk_audio.ps1 %1"

2 ответа

Решение

Try to formulate the command like this:

@="CMD.EXE /C Powershell.exe -File C:\\Users\\Me\\Desktop\\5min_chunk_audio.ps1 %1"

To complete harrymc's answer:

Windows Registry Editor Version 5.00

; set the name + icon in the context menu
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\audio\shell\ChunkAudio]
@="chunk audio"
"Icon"="%SystemRoot%\\System32\\shell32.dll,117"

; run the script
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\audio\shell\ChunkAudio\Command]
    @="CMD.EXE /K Powershell.exe -File C:\\Users\\Me\\Desktop\\5min_chunk_audio.ps1 \"%1\""

● CMD.EXE /C Carries out the command specified by string and then terminates

CMD.EXE /K Carries out the command specified by string but remains (good to debug)

type cmd /? in a cmd windows to find more args.

%1 is the first argument (%2 the second...). %1 contains the path of the file. We need to quote the arg to use it ( otherwhise the path will be cut at the first space ). Но "%1" won't work because we need to escape the quote, so we write \"%1\" ,


Bonus: To get the arg (the path) on your script simply use $arg (it's an "Automatic Variables". To know more run: Get-Help about_Automatic_Variables )

Другие вопросы по тегам