Converting scripts into functions

A function is a reusable, callable code block(s). A function can accept parameters and can produce different results.

A typical anatomy of a PowerShell function looks like this:

function Do-Something
{
<#
       comment based help
    #>
param
    (
#parameters
    )
#blocks of code
}

To illustrate, let's create a very simple function that takes a report server URL and lists all items in that report server.

Note

When naming functions, it is recommended that you adhere to the verb-noun convention. The verb also needs to be approved; otherwise, you may get a warning similar to when you import the SQLPS module:

WARNING: The names of some imported commands from the 'sqlps' module include unapproved verbs that might make them less discoverable. ...

Get SQL Server 2014 with PowerShell v5 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.