Windows Classroom Series – 16/Oct/2020

Functions in Powershell

  • They help in increasing reusability.
  • Functions can be written and put in the modules path ‘$env:PSModulePath’
  • Naming:
    • Generally it is a good practice to name your functions using <ApprovedVerb>-<Prefix><Noun>
    • Examples:
      • Write ps functions for admin team to get the chrome process Get-AdmChromeProcess, Kill-AdmChromeProcess
    • Use Verbs from approved list Get-Verb
  • Writing a simple function to return the Version of Powershell
  • Function Syntax
function FunctionName {
    param (
        OptionalParameters
    )
    
}

How-To’s to be learnt

  • How to make function resuable from powershell terminal?
  • How to make use of parameters effectively?
    • Include types
    • Mandatory Parameters
    • Optional Parameters
  • How to document powershell functions?

Syntax for a Powershell function

  • The following is a syntax of powershell
function [<scope>]<name> [([type] $parameter1[, [type] $parameter2])]
{
    param([type]$parameter1[, [type] $parameter2] )
    dynamicparam { <statement list> }
    begin { <statement list> }
    process { <statement list> }
    end { <statement list> }
}
  • Begin Block: This block is executed before pipeline-processing starts.
  • Process Block: The contents of Process block run once for each value received in the pipeline. The $_ variable to access objects
  • End Block : This block will be executed after all the objects in the process block have been processed
  • Refer Here for the samples created in class.

Next Topic

  • Windows Active Directory.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Customized Social Media Icons from Acurax Digital Marketing Agency

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube