Module 6: Functions, Filters Modules and Snap in
Module 6: Functions, Filters Modules and Snap in
Functions:
• Functions are self-defined new commands consisting of general PowerShell building blocks
• Functions can make work easier by combining several steps
• We can use functions to avoid code redundancy
• Functions are usefull when there are more than one step to execute
• Syntax:
Function <Function_Name>(<arg1>,<arg2>…..)
{
<PowerShell commandlets,PowerShell script blocks, conditional statements etc..>
}
• Arbitrary Arguments: $Args variable contains all the arguments that are passed to a
function
• Named Arguments: A function can assign a fixed name to arguments
• Predefined arguments: Arguments may include default values
• Typed arguments: Arguments can bed defined for particular data types
• Special argument types: Aside from conventional data types
PS Snapins:
• Windows PowerShell snap-in is Microsoft .NET framework assembly that contains
providers/cmdlets
• Windows PowerShell contains basic snap-ins and we can add custom snap-ins contains
provides/cmdlets
• When we add snap-ins , the cmdlets and providers that it contains immediately available
in the current session
• To add snap-in in all the PowerShell sessions, add it to the profile
• Get-PSSnapin –Registered
• Add-PsSnapin <name>
• Remove-psSnapin <name>
• Get-command –Module <Snapin name>
Modules:
• A module is a package that contains Windows PowerShell commands such as cmdlets,
Providers, functions, workflows, variables, and aliases
• All cmdlets and providers are added to PowerShell session using Modules or Snap-Ins
• Beginning in PowerShell 3.0 Windows PowerShell imports modules automatically the first
time that you use any command in an any installed module
• Get-Command cmdlet gets all cmdlets in all installed modules, even they are not yet in
the session