1

I have written a PS script, which replaces a specific string at the beginning of the file, adds another piece of string to the end of the file, and finally it puts out an XML.

My code might be ugly (I am not a programmer/engineer or anything, just trying to make life easier for some family members who are running a small business), but it works:

$content = Get-Content -Path 'C:\Users\blabla\Desktop\4440341930.txt'
$newContent = $content -replace 'text to be replaced','this is going to replace stuff'
$newContent | Set-Content -Path 'C:\Users\blabla\Desktop\4440341930.txt'
Add-Content C:\Users\blabla\Desktop\4440341930.txt '</Items>'
$x = [xml](Get-Content "C:\Users\blabla\Desktop\4440341930.txt")
$x.Save("C:\Users\blabla\Desktop\4440341930.xml")

I would like them to be able to run this script from the context menu, by right clicking on a txt file. I did a little research and I kind of get what I have to add to Registry, however, I'm not sure how to make it work. Since the path of each file that they are going to right click on is going to be different, the path that I'm specifying in $content is not going to work.

What do I have to modify in my code to be able to add it to the Registry?

5
  • Hey and good morning. I may be a little confused in regards to what you're trying to accomplish so bare with me. You just want to have the option to run the ps1 from doubleclicking it? Commented Feb 14, 2021 at 14:21
  • and 2nd question, is the text file always gonna be the same name? Commented Feb 14, 2021 at 14:22
  • Using -replace on what is really an XML file is a bad idea and can likely ruin the xml file, which will result in errors on $x = [xml](Get-Content "C:\Users\blabla\Desktop\4440341930.txt") Commented Feb 14, 2021 at 14:26
  • Hi, basically we're getting a very badly formatted txt file from another company, which is supposed to be an xml, and with my modifications we are going to be able to import the xml into Excel. The file is always going to look the same, so I doubt that we would run into any issues regarding this. I tested it with multiple files that we've received in the past. My main problem is that I don't know how I could add this particular script to the context menu, since I always have to modify the file path in the script. Commented Feb 14, 2021 at 14:42
  • Basically I would like to somehow pass the txt path information to the script (the location of the file that we're right clicking), and run the script on it, which is going to generate the fixed xml. Commented Feb 14, 2021 at 14:43

1 Answer 1

5

To accomplish this you need to:

  1. Create a Shortcut in the SendTo Folder: "$DestinationPath\AppData\Roaming\Microsoft\Windows\SendTo"
  2. The target: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
  3. The Arguments: -File "d:\path\your PS1 file"
  4. In your program read the file name passed by Explorer as:
Param
(
   [Parameter(Mandatory=$false)]
     [String] $FilePath
)

I've written a Setup Function that accomplishes steps 1-3 that I include in all my programs that I want on the context menu and then just run the program with the -Setup switch. We're not supposed to post developed code here, but if you can't figure it out let me know and I'll post it and hope I don't get killed for it. LOL!

UPDATE:

If you want to pass more than one file you need to process the files a little differently. Delete the Param block above and then use this type of code to retrieve the files.

If ($Args.count -eq 0) {
  
    $Message = "No Files were passed from File Explorer."
    [Void]$MsgBox::Show(
      "$Message","System Exit",$Buttons::OK, $MBIcons::Stop)
    Show-PowerShell
    Exit             #Comment out for testing from ISE!
  }
  Else {
    $FilesToCopy = $Args
  }
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, I didn't even have to mess with the registry this way.
Wow this is a great approach, I spent an hour trying to get it working with the registry, to no avail.
FoxDeploy, glad you liked it. I don't know if you remember but back in 2017 you helped my with a PS program I was writing to retrieve information about the computer and you introduced me to using XAML for my forms. It was a great help and glad to return a little.
@RetiredGeek Can this be done with multiple files selected? As far as I can tell, if I select multiple files and use Send To, only one of the files gets sent as the script argument.
@gargoylebident YES you can, see the updated answer above.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.