PowerShell Station

Mike's PowerShell Musings

Menu

Skip to content
  • About Mike
  • Blogs
  • Books
  • Downloads!
  • Glossary
  • IDEs
  • Policies
  • Sites
  • Tools

Def: A Quick Helper Function

Posted by mike on 7 July 2012, 3:11 am

Did you ever find yourself knowing that you had written a function but you couldn’t remember which module you put it in? If you have the module imported, you can find out with this:

dir function:functionname | select Name,ModuleName

Note that I am defiantly using the “dir” alias for get-childitem even though everyone says not to. I’ll be a rulebreaker again later on in this post as you’ll see.

If you ever wanted to know the definition of a function you can use the “definition” property:

dir function:functionname | select -expand Definition

I recently put these 2 together in a helper function I call “def”

function def{
param($funcname)
    $func=dir function:$funcname
    if($func.ModuleName){
      Write-host "$func is defined in module $($func.ModuleName)"
    }
    write-host $Func.definition
}

Here you’ll see my rebellious ways in 2 different strains:

  • I defined a function without using the verb-noun convention
  • I used write-host to provide output from a function

For what it’s worth, I am very fond of the verb-noun naming convention. I was tempted to name the function “show-commanddefinition” and give it an alias of “def”, but I doubt I’d ever use the full name.

Also, I completely agree with not using write-host to provide output from functions. Doing so makes the function in question a dead-end as far as PowerShell goes. In this case, I used it because I was providing 2 different kinds of output (the module name and the definition of the function) and writing those to the output stream wouldn’t help much. I could have defined an object with those 2 properties, or even just used select-object to whittle down the command object to those 2, but then the default display would have made presented a tabular view which probably would have truncated the definition. This function wasn’t meant to be consumed by other functions, it was meant for me to look at the output. Thus, write-host was the right choice. Jeff Hicks is correct, though, every time you use write-host in a function (let’s say a “real function”), %deity% kills a puppy.

Fortunately for everyone I know, that’s about as rebellious as I get.

Let me know what you think.
Mike

Filed under Scripts | Tagged function, metadata, PowerShell, Script | Permalink

Post navigation

« A Remoting Issue with PowerShell 3 Beta
PowerShell Splatting Tricks »

Archives

  • February 2021 (1)
  • September 2020 (1)
  • August 2020 (2)
  • January 2019 (1)
  • August 2018 (2)
  • July 2018 (2)
  • June 2018 (8)
  • February 2018 (1)
  • January 2018 (1)
  • October 2017 (8)
  • July 2017 (2)
  • May 2017 (2)
  • April 2017 (6)
  • February 2017 (3)
  • January 2017 (5)
  • August 2016 (7)
  • July 2016 (3)
  • May 2016 (4)
  • April 2016 (8)
  • March 2016 (2)
  • February 2016 (1)
  • January 2016 (10)
  • December 2015 (3)
  • November 2015 (2)
  • October 2015 (4)
  • September 2015 (3)
  • August 2015 (3)
  • May 2015 (1)
  • April 2015 (1)
  • January 2015 (2)
  • December 2014 (1)
  • November 2014 (2)
  • October 2014 (1)
  • December 2013 (2)
  • November 2013 (1)
  • October 2013 (3)
  • May 2013 (3)
  • April 2013 (2)
  • March 2013 (1)
  • July 2012 (1)
  • April 2012 (1)
  • March 2012 (1)
  • February 2012 (1)
  • December 2011 (1)
  • October 2011 (1)
  • August 2011 (1)
  • May 2011 (2)
  • January 2011 (2)
  • October 2010 (1)
  • August 2010 (3)
  • May 2010 (2)
  • April 2010 (1)
  • January 2010 (3)
  • December 2009 (2)
  • November 2009 (4)
  • October 2009 (3)
  • September 2009 (8)

Tags

ADO.NET ADOLib alias Books bug CmdletBinding Convert-WindowsImage ErrorHandling ETS Exceptions get-command get-learning HomeLab Hyper-V ISE Learning MidMO Non-Terminating Errors Package parameter Parameter Function Parameters Pipeline POSH_ADO position PositionalBinding PowerShell Require Script scripts source-control SQL SQLPSX SQL Server STLPSUG SWMOPSUG syntax Terminating Errors Tools UserGroups VB.NET Visio VisioBot3000 Voodoo WPFBot3000

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
© 2016 PowerShell Station