PowerShell Not your Father’s Command Line Part 7 of 31: Conjunction Function PowerShell What Are Functions?

imageThis post is designed to introduce what a Function is and basic primary purpose of functions in PowerShell.  Tomorrow Sarah will go into more detail on how to show the real power behind functions!  For the purpose of this post I am going to focus on some of the basics to get you started with the built-in functions and even create a simple function.  At the end of the post I we will take a quick look at the most useful function (imho).  To get started functions like aliases are designed to help you leverage your existing command prompt/shell knowledge.  Here is an example of a built-in function:


I know what you are thinking, wait a second that is just the command to change to my C drive, and it is.  However, in PowerShell the actual command that runs behind the scenes is Set-Location C:.  Even though this seems like an alias it does one key thing aliases cannot and that is functions can accept parameters.  This is just one of the great things functions can do.  There are several built-in functions and to see a list of all the functions you can run the following command:

Get-Command –type function

Functions can do all kinds of fantastic and complex things in PowerShell, but they do not have to be complex to be useful.  Sometimes you just want to create a shortcut similar to an alias for a common command, and because functions accept parameters this allows more flexibility to allow PowerShell to work they you want it to work. Creating a function is pretty straight forward, you just need to know what you want to do and the name you want to call the function.  The basics of creating a function follows this syntax function <function-name> {statements}  The statements can be PowerShell cmdlets or more complex scripting techniques.  I recommend for consistency you use a function name in the style of a PowerShell cmdlet.  Functions are also like aliases, they are temporary and on Monday’s post we will (finally) see how to work with profiles and to make tools like functions/aliases permanent!  If you wanted to create a simple function called Start-Shadow to start the Volume Shadow Copy service you would type something like this (I used the verbose switch so you can see a little bit of output from the command):

Function Start-Shadow {Start-Service VSS -verbose}

To close the post I would like to share with you probably most useful function built-in to PowerShell is a function called TabExpansion.  This function is called every time you hit the Tab key and is commonly known as Tab autocomplete.  Tab autocomplete allows you to make sure you get the spelling of your cmdlets (remember PowerShell is spelling sensitive), parameters and variables correctly when you are writing your PowerShell commands.   In a PowerShell session if you begin typing a cmdlet and hit your tab key the function will cycle through all of the possibilities of the command combination you started with.  So if you start to type this command Get-A then hit TAB you could see acl, alias, authenticodesignature (based on what modules you have loaded) To learn more about TabExpansion check out this older post off the PowerShell teams blog: The new TabExpansion feature...   There is also a great little tool called PowerTab that will take Tab Autocomplete to another level, to learn more take a look here: http://powertab.codeplex.com/ 

Again thanks for reading and if you missed any of the previous posts you can find a master list of series postings located here: PowerShell Not Your Father's Command Line: 31 Days of PowerShell or on Sarah’s blog here: PowerShell Not Your Father's Command Line: 31 Days of PowerShell.  Lastly Sarah and I want to hear from you email either of us with your comments or suggestions for future postings let us know, we look forward to hearing from you. Have a great day!

Comments (2)
  1. Keith Elder says:

    Matt, I'm missing some connecting dots on powershell. Ok, so I write a function or wrote a command. Where do I put these so it gets picked up automatically? How could I deploy these functions and even commands so other developers can leverage them? For example, on Unix you put scripts and these like this in /usr/bin/ and make it chmod 755 to be executable and then you can run it. How does this apply to Powershell? I think we need a post on these connecting dot kind of things.

  2. Matt Hester says:

    Good afternoon Keith, thank you for reading the post and thank you for the question.  I believe the functionality you are looking for is profiles.  We talked about profiles in part9: blogs.technet.com/…/powershell-not-your-father-s-command-line-part-9-of-31-another-side-of-powershell-profiles.aspx In the post I talked about a profile for the current user and just one session.  If it’s for all users on a machine to use, then you can use the $profile.AllUsersAllHosts file.  I also chatted wiht Sarah and she mentioned if you are talking multiple computers in a domain, then if I had to guess, you could probably store the files in a script module (.psm1 file) and use a login script to deploy the module to others’ machines and somehow include that module in the $profile.AllUsersAllHosts so that it’s available in all sessions to all users.  Hopefully that helps.

Comments are closed.

Skip to main content