Making an executable take pipelined input in PowerShell

It annoys me when I am working with PowerShell try to pipe the results of a cmdlet into a exe which doesn’t understand pipelined data.  To solve this problem I began aliasing some of the common programs I would like to pipe data to with custom functions.

For example, I have the following function in my profile to alias notepad.exe:

function n($file) {
  if($file -ne $null){
    notepad $file
  } else {
    notepad $input
  }
}

I can use the function named n whenever I want to open a file using notepad. This function will take a file name either through piping or as a regular argument.

So I can open a file in notepad this way:

n someFile.txt

or this way:

ls *.txt | n

You can create similar functions for other programs as well.  Here is the one I use for Emacs:

$emacs = "emacs.exe -nw --no-splash"
function emacs($file){
  if($file -ne $null){
    Invoke-Expression "$emacs $file"
  }
  else {
    Invoke-Expression "$emacs $input"
  }
}
This entry was posted in PowerShell. Bookmark the permalink.

One Response to Making an executable take pipelined input in PowerShell

  1. I’m extremely impressed with your writing skills
    and also with the layout on your weblog. Is this a paid theme or did you customize it yourself?
    Anyway keep up the nice quality writing, it’s rare to see a nice blog like
    this one today.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>