Previously we looked at some of the different ways one can extend Windows PowerShell. Now we'll learn some basic for actually using these methods.

You may

Requires Free Membership to View

notice that I've skipped creating snap-ins here, as that requires developer skills. Also, we will cover modules and advanced functions in detail in a future article.

Functions

Functions are one of the elementary building blocks of Windows PowerShell, and they can vary in complexity from a single line to full-blown modules. Still, the basic format and components remain the same.

The most basic syntax is:
function < name> { < code> }

The standard format contains four basic constructs, which are Param and three script-blocks or clauses -- Begin, Process and End.

  • Param -- Defines parameters for the function. If not provided, the function will use groups the parameters passed to the function in a variable called $args.
  • Begin (script-block) -- Begin is used for function setup. This code is only called the first time a function is initialized.
  • Process (script-block) -- Process is used in a pipeline and is processed for every item in the pipeline. You reference the current item using "$_".
  • End (script-block) -- End is ... well, the end. Just like Begin, it is only processed once, but instead of during initialization it is processed right before the code is completed. It is also the default block, which means if you do not specify any code blocks it implies that all the code is the end block.

Example of syntax:
Function < name>
{
     Param()

     # Script Blocks
     Begin{}
     Process{}
     End{}
}

 

Note: Notice the {} on the end of each script block. The code is placed in between. For example: Process { Write-Host "Processing element $_" }

For more details try Get-Help about_functions.

Filter

I almost didn't want to mention these, but I decided it would be a disservice to the reader not to. Filters are not used very often in Windows PowerShell and are really not needed. This is just like a function but all the code is run as "Process".

Again, for more details try Get-Help about_functions.

Scripts

A script in its simplest form is one or more Windows PowerShell commands saved in a .ps1 file. There is the famous "Hello World" example in PowerShell, which is very much like a batch file except it can support the same constructs as a function. In actuality, you can simply remove the function  {} from a function and save it in a file to have a script:

Param($Name)
Begin{"Initializing Script with Parameter $Name"}
Process{" You just passed $_ to me"}
End{" Jobs done… lets cleanup"}

For more details, try Get-Help about_scripts.

Profile

A profile is basically just a Windows PowerShell script that is processed every time PowerShell is started. This is similar to autoexec.bat from back in the old days. We've covered profiles in Windows PowerShell before, so I won't rehash it again here. I will let you in on a little known fact though: PowerShell has a built-in variable with all the profile locations wrapped up nicely for you. This can be used to create and edit your profile:

  • $Profile -- This is the built-in variable that contains information about profile location. It contains four useful properties discussed below.
  • $Profile.AllUsersAllHosts -- Contains the location of the All Users profile (Global) for all PowerShell hosts.
  • $Profile.AllUsersCurrentHost -- Contains the location of the All Users profile (Global) for the current PowerShell host.
  • $Profile.CurrentUserAllHosts -- Contains the location of the Current User profile for all PowerShell hosts.
  • $Profile.CurrentUserCurrentHost -- Contains the location of the Current User profile for the current PowerShell host.

To wrap up, these are just a few ways you can customize and extend Windows PowerShell for your own use. By using functions, scripts, and creating a custom profile, you can start down the road to nirvana when it comes to an automation environment.

Missed a column? Check out our Scripting School archive.

ABOUT THE AUTHOR
Brandon Shell has been in the IT industry since 1994. He started out as a PC tech and general fix-it guy for numerous companies. In 2007, he joined the PowerShell MVP ranks, and Shell has spent the past several years building his PowerShell knowledge and helping others build theirs.


This was first published in April 2010

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.