Scott Chadwick
Scott Chadwick

Reputation: 11

Pass the names of the selected files from "Send To" menu to powershell script

I have a script that I wrote that is added to the "Send To" menu already, and it asks for a search string and then searches every .log and .txt file in that directory for the string.

But what I need it to do is look at the file I am right clicking on and collect the name so I can use it as the only file to search in.

So if you can help me grab the name of the file I am right clicking on, and then "send to" my PowerShell script. Thanks

Upvotes: 1

Views: 1112

Answers (1)

zett42
zett42

Reputation: 27776

Create a shortcut in your "SendTo" folder:

  • Press Win+R, enter shell:sendto
  • Create a shortcut with this target:
    • for Windows PowerShell:
      "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -File "FullPathToYourScript.ps1"
      
    • for PowerShell 7+:
      "%ProgramFiles%\PowerShell\7\pwsh.exe" -File "FullPathToYourScript.ps1"
      

When the "Send to" menu item is invoked, the system appends the full paths of the selected files to the commandline. In your PowerShell script, use the automatic $args array variable to get the file paths from the commandline arguments.

This demo shows how you can get the number of selected files and loop over the paths:

"Received $($args.Count) files:"

foreach( $path in $args ) {
    "Processing file: $path"    
}

Upvotes: 3

Related Questions