PowershellGuru
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login
No Result
View All Result
PowershellGuru
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login
No Result
View All Result
PowershellGuru
No Result
View All Result
Home Powershell Fundamentals

A step-by-step guide for function in Powershell in 2021

themeanmachine19@gmail.com by [email protected]
September 1, 2021
in Powershell Fundamentals
0
Function in PowerShell
Share on FacebookShare on Twitter

You might also like

Powershell filter

Powershell filter: Using where-object and select-object (2021)

September 9, 2021
error handling in powershell

Understanding Error handling in Powershell [2021]

August 20, 2021

In order to reuse the same code in multiple scripts, PowerShell functions are used.
PowerShell functions are a collection of PowerShell statements that have been given a name by the Whenever we want to run a function, we need to type its name.
Functions can have parameters, just like cmdlets. Pipeline or command-line access to function parameters is possible.
They return a value that can be assigned to variables or passed as a command-line argument or function parameter. In order to specify a return value, we can use the keyword return

Function in PowerShell

Function Syntax

Below is the syntax used for Function.

				
					function [<scope:>]<name> [([type]$parameter1[,[type]$parameter2])]  
{  
  param([type]$parameter1 [,[type]$parameter2])  
  dynamicparam {<statement list>}  
  begin {<statement list>}  
  process {<statement list>}  
  end {<statement list>}  
}  
				
			

The following terms are included in the syntax above:

  1. A function keyphrase
  2.  A name that you have chosen
  3. Scope of Function (It is optional)
  4. There can be any number of named parameters.
  5. One or more PowerShell commands are enclosed in braces.

Example of a Function:

				
					function Operation{
   $num1 = 8
   $num2 = 2
   Write-Host "Multiply : $($num1*$num2)"
   Write-Host "Addition : $($num1+$num2)"
   Write-Host "Subtraction : $($num1-$num2)"
   Write-Host "Divide : $($num1 / $num2)"
}
Operation



				
			

Results:

				
					Multiply : 16
Addition : 10
Subtraction : 6
Divide : 4
				
			

Function Scope

  • A function exists in the scope in which it was created in PowerShell.
  • If a function is contained within a script, it is only available to the statements contained within that script.
  • When we specify a function in the global scope, we can use it in other functions, scripts, and commands.

Advance Function in Powershell

Advanced functions are functions that can perform operations that are similar to those performed by cmdlets. When a user wants to write a function without having to write a compiled cmdlet, they can use these functions.

The primary distinction between using a compiled cmdlet and an advanced function is that compiled cmdlets are.NET Framework classes that must be written in a.NET Framework language. Furthermore, the advanced functions are written in the PowerShell scripting language.

The following example shows how to use PowerShell’s advanced function:

				
					function show-Message  
 {  
     [CmdletBinding()]  
     Param (  
     [ Parameter (Mandatory = $true)] 
       
         [string] $Name  
     )    
     Process  
     {  
         Write-Host ("Hi $Name !")  
         write-host $Name "today is $(Get-Date)"
     }  
 }  

 show-message
				
			

Results:

				
					cmdlet show-Message at command pipeline position 1
Supply values for the following parameters:
Name: Dhrub
Hi Dhrub !
Dhrub today is 09/01/2021 13:41:12
				
			

Conclusion

We use the function in every language and it usually brings down the number of lines a code has. If your code is 1000 lines then with the help of Function you can bring down the count to 500.Hope you have enjoyed this post, will see you in our next post.

Please login to join discussion
close

DON’T MISS A POST

Keep up to date with PowershellGuru

Powershell Blogs

PowershellGuru provides the best PowerShell scripts available that can be used and download freely. Do Check our blogs to get updated regularly.

Check your inbox or spam folder to confirm your subscription.

themeanmachine19@gmail.com

[email protected]

Related Stories

Powershell filter

Powershell filter: Using where-object and select-object (2021)

by [email protected]
September 9, 2021
0

Step-by-step guide for function in PowerShell. Function in Powershell. Function with the return value. Function scope. Function examples.

error handling in powershell

Understanding Error handling in Powershell [2021]

by [email protected]
August 20, 2021
0

Error handling in Powershell. Error handling in Powershell . Error handling in Powershell. PowershellGuru. Try block. Throw block. Catch block....

Useful Powershell commands

10 Useful Powershell commands that everyone should know

by [email protected]
August 10, 2021
0

Powershell cmdlets. Powershell cmdlets list. Powershell cmdlets example. Get-process. Get-command. Set-executionpolicy. Clear-history. Stop-process.

Pipeline in Powershell

3 Proven ways to Learn Pipeline in Powershell

by [email protected]
July 4, 2021
0

Pipeline in Powershell. Powershell Pipeline. select-object. sort-object. measure-object. Learn Pipeline in Powershell. PowershellGuru.

Next Post
Powershell filter

Powershell filter: Using where-object and select-object (2021)

Please login to join discussion

Recommended

Detecting Log4j vulnerable servers using PowerShell

Detect Log4j vulnerable servers using PowerShell

December 29, 2021
How to find NTP Server using PowerShell?

How to find NTP Server using PowerShell?

November 27, 2021

About

Dhrub Bharali

PowerShell Enthusiast

Dhrub is hardcore Powershell enthusiast, he has wriiten more than 100 powershell scripts and he is the sole owner of PowerShellGuru.

Follow Us

Popular Story

  • Installing software remotely using powershell

    Easy way to install software remotely using PowerShell (2021)

    827 shares
    Share 331 Tweet 207
  • Detect Log4j vulnerable servers using PowerShell

    753 shares
    Share 301 Tweet 188
  • How to find NTP Server using PowerShell?

    738 shares
    Share 295 Tweet 185
  • Get-LocalGroupMember: Find Local admin using PowerShell (2021)

    728 shares
    Share 291 Tweet 182
  • Get installed software list quickly using PowerShell (2021)

    695 shares
    Share 278 Tweet 174
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login

© 2022 PowershellGuru- PowerShell Scripts For Automation

No Result
View All Result
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login

© 2022 PowershellGuru- PowerShell Scripts For Automation

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.