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: 

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!