Functions
Functions work similarly to script blocks, but are named:
function Get-Fullname($firstname, $lastName)
{
Write-Host ($firstName + " " + $lastName)
}
Get-Fullname "Conner" "Taylor"
Can pass an argument by reference, but it will be turned into an object, thus requiring the .Value syntax:
function Set-FVar([ref] $myparam)
{
$myparam.Value = 33
}
Clear-Host
$fvar = 42
"Before Set-FVar: $fvar"
Set-FVar ([ref] $fvar) # Must add ref to call
"After Set-FVar: $fvar"
Can enable pipelining with process just like with script blocks:
function Get-TextFiles ()
{
begin { $retval = "Here are the text files: `r`n" }
process
{
if ($_.Name -like "*.txt")
{
$retval = $retval + "`t" + $_.Name + "`r`n"
}
}
end { return $retval }
}
Clear-Host
Set-Location "C:\Temp"
Get-ChildItem | Get-TextFiles
Can use filter to remove unwanted objects:
filter Show-TextFiles
{
$filename = $_.Name
if ($filename -like "*.txt")
{
return $_
}
}
Set-Location "C:\Temp"
Clear-Host
Get-ChildItem | Show-TextFiles
Example handling switches and writing to different types of output:
function Get-ChildName()
{
param([switch]$verbose, [switch]$debug)
if ($verbose.IsPresent)
{
$VerbosePreference = "Continue" # $VerbosePreference is a special powershell var
}
else
{
$VerbosePreference = "SilentlyContinue" # Default value
}
if ($debug.IsPresent)
{
$DebugPreference = "Continue"
}
else
{
$DebugPreference = "SilentlyContinue"
}
Write-Verbose "Current working location is $(Get-Location)"
Write-Output (Get-ChildItem | Select-Object "Name")
Write-Debug "Files found."
}
Set-Location "C:\Temp"
Get-ChildName -verbose -debug | Where-Object {$_.Name -like "*.txt"}
Can add custom help to a function, will be displayed in Get-Help:
function Get-ChildName()
{
<#
.SYNOPSIS
Returns a list of only the names for the child items in the current location.
.DESCRIPTION
This function is similar to Get-ChildItem, except that it only returns the name property.
.INPUTS
None.
.OUTPUTS
System.String. Sends a collection of strings out the pipeline.
.EXAMPLE
Example 1 - Simple use
Get-ChildName
Example 2 - Passing to another object in the pipeline
Get-ChildName | Where-Object {$_.Name -like ".txt"}
.LINK
Get-ChildItem
#>
param([switch]$verbose, [switch]$debug)
if ($verbose.IsPresent)
{
$VerbosePreference = "Continue" # $VerbosePreference is a special powershell var
}
else
{
$VerbosePreference = "SilentlyContinue" # Default value
}
if ($debug.IsPresent)
{
$DebugPreference = "Continue"
}
else
{
$DebugPreference = "SilentlyContinue"
}
Write-Verbose "Current working location is $(Get-Location)"
Write-Output (Get-ChildItem | Select-Object "Name")
Write-Debug "Files found."
}
Use trap to catch and handle errors:
function Divide-Numbers($num, $denom)
{
Write-Host "Begin division."
$result = $num / $denom
Write-Host "Result: $result"
Write-Host "End division."
trap [System.DivideByZeroException]
{
Write-Host "You aren't powerful enough to divide by zero!"
continue
}
trap
{
Write-Host "Error:"
Write-Host $_.ErrorID
Write-Host $_.Exception.Message
continue # Continues with the next line of code after the error
}
}
Clear-Host
Divide-Numbers 33 11
Divide-Numbers 33 0