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
Apple Software Solutions Page 1
Filters:
• The slow sequential mode of pipeline resulting in enormous consumption of memory
• If we use rapid streaming mode of pipeline, preceding commands of pipeline can process
with minimal memory
• Use “Filter” key work instead of “Function” for rapid streaming mode pipeline functions
• Use $_ instead of $input to process the current result object of preceding command
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
Apple Software Solutions Page 2
• Only modules that are stored in the location specified by the PSModulePath environment
variable are automatically imported.
• Modules in other locations must be imported by using the Import-Module cmdlet
• Commands that use Windows PowerShell providers do not automatically import a module.
For example, if you use command that requires the WSMan: drive, such as Get-
PSSessionConfiguration cmdlet, you might need to use the import-module cmdlet to
import the Microsoft.WSMan.Management module that includes the WSMan: drive.
• Use import-module to import and use $PSModuleAutoLoadingPreference variable to
enable, disable and configure automatic importing of modules.
• If you receive a module as a folder with files in it, you need to install it on computer
before using
• Windows PowerShell comes with several modules pre-installed
• In windows server 2008 R2, the add-features wizard in server manager automatically
installs the feature modules that you select.
• Many other modules come in any installer or setup program that installs the module
Module Locations and $PsModulePath
• System: $pshome\Modules (%windir%\System32\WindowsPowerShell\v1.0\Modules)
• Current User: $home\Documents\WindowsPowerShell\Modules
(%UserProfile%\Documents\WindowsPowerShell\Modules)
• Get default module location using $env:psmodulepath environment variable
• To add default module location, type: $env:psmodulepath = $env:psmodulepath +
";<path>"
• To find modules that are installed in default module location, but not yet imported into
your session, type : Get-Module -ListAvailable
• To find modules that have already been imported into your session, type: Get-Module
• To find all commands available in a module, type: Get-Command -Module <Module Name>
Apple Software Solutions Page 3