SlideShare a Scribd company logo
PowerShell Functions Presented by Mike Pfeiffer http://www.AZPoSH.com
Agenda PowerShell Functions What is a PowerShell Function? Structure of a PowerShell Function Function Guidelines Input Parameters Output Scope Functions in the Pipeline Begin, Process and End Blocks Parameter Attributes Comment Based Help
What is a PowerShell Function? Think of it as a block of code that behaves as a cmdlet Can be loaded from your profile, dot sourced via a script, or imported from a module Can be called or defined in another function or script
Structure of a PowerShell Function
Function Guidelines Must be defined before they are called Call functions without parenthesis or commas: Get-MyStuff –Param1 $value1 –Param2 $value2 Get-MyStuff $value1 $value2
Input Parameters The $args variable Named and positional parameters Parameter type constraints Initializing parameters with default values The switch parameter
Output Write-Host is for writing to the console Use Write-Output to return function output Don’t use the Return keyword to return output
Scope Functions have internal scope Don’t use or modify variables from parent scope Consider using default values for internal variables
Functions in the Pipeline The $input variable Filters
Begin, Process and End Blocks
Parameter Attributes Parameter Attribute Declaration (MSDN): http://bit.ly/bzGu34
Comment Based Help
Resources Get-Help About_Functions Get-Help About_Functions_Advanced Get-Help About_Comment_Based_Help PowerShell in Action http://manning.com/payette2/ PowerGUI PowerShell v2 Snippets http://bit.ly/azDkJG
Thank You!

More Related Content

What's hot (20)

Spring Boot Tutorial
Spring Boot TutorialSpring Boot Tutorial
Spring Boot Tutorial
Naphachara Rattanawilai
 
[Question Paper] Linux Administration (75:25 Pattern) [November / 2014]
[Question Paper] Linux Administration (75:25 Pattern) [November / 2014][Question Paper] Linux Administration (75:25 Pattern) [November / 2014]
[Question Paper] Linux Administration (75:25 Pattern) [November / 2014]
Mumbai B.Sc.IT Study
 
Java Networking
Java NetworkingJava Networking
Java Networking
Sunil OS
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
WebStackAcademy
 
Spring Boot in Action
Spring Boot in Action Spring Boot in Action
Spring Boot in Action
Alex Movila
 
Maven ppt
Maven pptMaven ppt
Maven ppt
natashasweety7
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
LivePerson
 
PowerShell-1
PowerShell-1PowerShell-1
PowerShell-1
Saravanan G
 
Closures in Javascript
Closures in JavascriptClosures in Javascript
Closures in Javascript
David Semeria
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 
Clean code
Clean codeClean code
Clean code
Arturo Herrero
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
JAVA
JAVAJAVA
JAVA
Axel KAMALAK
 
DOT Net overview
DOT Net overviewDOT Net overview
DOT Net overview
chandrasekhardesireddi
 
Laravel Tutorial PPT
Laravel Tutorial PPTLaravel Tutorial PPT
Laravel Tutorial PPT
Piyush Aggarwal
 
Facade Design Pattern
Facade Design PatternFacade Design Pattern
Facade Design Pattern
Livares Technologies Pvt Ltd
 
Creating an executable jar file
Creating an executable jar fileCreating an executable jar file
Creating an executable jar file
Ankush Srivastava
 
Cours JavaScript
Cours JavaScriptCours JavaScript
Cours JavaScript
Olivier Le Goaër
 
Cours php & Mysql - 1ére partie
Cours php & Mysql - 1ére partieCours php & Mysql - 1ére partie
Cours php & Mysql - 1ére partie
kadzaki
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
swapnac12
 
[Question Paper] Linux Administration (75:25 Pattern) [November / 2014]
[Question Paper] Linux Administration (75:25 Pattern) [November / 2014][Question Paper] Linux Administration (75:25 Pattern) [November / 2014]
[Question Paper] Linux Administration (75:25 Pattern) [November / 2014]
Mumbai B.Sc.IT Study
 
Java Networking
Java NetworkingJava Networking
Java Networking
Sunil OS
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
WebStackAcademy
 
Spring Boot in Action
Spring Boot in Action Spring Boot in Action
Spring Boot in Action
Alex Movila
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
LivePerson
 
Closures in Javascript
Closures in JavascriptClosures in Javascript
Closures in Javascript
David Semeria
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
Creating an executable jar file
Creating an executable jar fileCreating an executable jar file
Creating an executable jar file
Ankush Srivastava
 
Cours php & Mysql - 1ére partie
Cours php & Mysql - 1ére partieCours php & Mysql - 1ére partie
Cours php & Mysql - 1ére partie
kadzaki
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
swapnac12
 

Viewers also liked (20)

Intro to PowerShell Workflow
Intro to PowerShell WorkflowIntro to PowerShell Workflow
Intro to PowerShell Workflow
Jeffery Hicks
 
PowerShell crash course
PowerShell crash coursePowerShell crash course
PowerShell crash course
Concentrated Technology
 
Managing enterprise with PowerShell remoting
Managing enterprise with PowerShell remotingManaging enterprise with PowerShell remoting
Managing enterprise with PowerShell remoting
Concentrated Technology
 
PowerShell and the Future of Windows Automation
PowerShell and the Future of Windows AutomationPowerShell and the Future of Windows Automation
PowerShell and the Future of Windows Automation
Concentrated Technology
 
Implementing dr w. hyper v clustering
Implementing dr w. hyper v clusteringImplementing dr w. hyper v clustering
Implementing dr w. hyper v clustering
Concentrated Technology
 
PowerShell 8tips
PowerShell 8tipsPowerShell 8tips
PowerShell 8tips
Concentrated Technology
 
Combining output from multiple sources
Combining output from multiple sourcesCombining output from multiple sources
Combining output from multiple sources
Concentrated Technology
 
Managing SQLserver
Managing SQLserverManaging SQLserver
Managing SQLserver
Concentrated Technology
 
No-script PowerShell v2
No-script PowerShell v2No-script PowerShell v2
No-script PowerShell v2
Concentrated Technology
 
Ive got a powershell secret
Ive got a powershell secretIve got a powershell secret
Ive got a powershell secret
Chris Conte
 
PowerShell crashcourse
PowerShell crashcoursePowerShell crashcourse
PowerShell crashcourse
Concentrated Technology
 
PowerShell v4 Desired State Configuration
PowerShell v4 Desired State ConfigurationPowerShell v4 Desired State Configuration
PowerShell v4 Desired State Configuration
Jason Stangroome
 
Three cool cmdlets I wish PowerShell Had!
Three cool cmdlets I wish PowerShell Had!Three cool cmdlets I wish PowerShell Had!
Three cool cmdlets I wish PowerShell Had!
Thomas Lee
 
PowerShell crashcourse for sharepoint
PowerShell crashcourse for sharepointPowerShell crashcourse for sharepoint
PowerShell crashcourse for sharepoint
Concentrated Technology
 
PowerShell and WMI
PowerShell and WMIPowerShell and WMI
PowerShell and WMI
Concentrated Technology
 
Automating Active Directory mgmt in PowerShell
Automating Active Directory mgmt in PowerShellAutomating Active Directory mgmt in PowerShell
Automating Active Directory mgmt in PowerShell
Concentrated Technology
 
Automating ad with powershell
Automating ad with powershellAutomating ad with powershell
Automating ad with powershell
Concentrated Technology
 
PowerShell custom properties
PowerShell custom propertiesPowerShell custom properties
PowerShell custom properties
Concentrated Technology
 
Introduction to powershell
Introduction to powershellIntroduction to powershell
Introduction to powershell
Salaudeen Rajack
 
Meet Windows PowerShell
Meet Windows PowerShellMeet Windows PowerShell
Meet Windows PowerShell
Concentrated Technology
 
Intro to PowerShell Workflow
Intro to PowerShell WorkflowIntro to PowerShell Workflow
Intro to PowerShell Workflow
Jeffery Hicks
 
Managing enterprise with PowerShell remoting
Managing enterprise with PowerShell remotingManaging enterprise with PowerShell remoting
Managing enterprise with PowerShell remoting
Concentrated Technology
 
PowerShell and the Future of Windows Automation
PowerShell and the Future of Windows AutomationPowerShell and the Future of Windows Automation
PowerShell and the Future of Windows Automation
Concentrated Technology
 
Ive got a powershell secret
Ive got a powershell secretIve got a powershell secret
Ive got a powershell secret
Chris Conte
 
PowerShell v4 Desired State Configuration
PowerShell v4 Desired State ConfigurationPowerShell v4 Desired State Configuration
PowerShell v4 Desired State Configuration
Jason Stangroome
 
Three cool cmdlets I wish PowerShell Had!
Three cool cmdlets I wish PowerShell Had!Three cool cmdlets I wish PowerShell Had!
Three cool cmdlets I wish PowerShell Had!
Thomas Lee
 
Automating Active Directory mgmt in PowerShell
Automating Active Directory mgmt in PowerShellAutomating Active Directory mgmt in PowerShell
Automating Active Directory mgmt in PowerShell
Concentrated Technology
 
Introduction to powershell
Introduction to powershellIntroduction to powershell
Introduction to powershell
Salaudeen Rajack
 
Ad

Similar to PowerShell Functions (20)

Power shell training
Power shell trainingPower shell training
Power shell training
David Brabant
 
PowerShell Zero To Hero Workshop!
PowerShell Zero To Hero Workshop!PowerShell Zero To Hero Workshop!
PowerShell Zero To Hero Workshop!
Daisy Stevens
 
PowerShell Fundamentals
PowerShell FundamentalsPowerShell Fundamentals
PowerShell Fundamentals
mozdzen
 
PowerShell Core Skills (TechMentor Fall 2011)
PowerShell Core Skills (TechMentor Fall 2011)PowerShell Core Skills (TechMentor Fall 2011)
PowerShell Core Skills (TechMentor Fall 2011)
Concentrated Technology
 
Windows Powershell in Action 1st Edition Bruce G. Payette
Windows Powershell in Action 1st Edition Bruce G. PayetteWindows Powershell in Action 1st Edition Bruce G. Payette
Windows Powershell in Action 1st Edition Bruce G. Payette
takabedembe
 
Windows Powershell in Action 1st Edition Bruce G. Payette
Windows Powershell in Action 1st Edition Bruce G. PayetteWindows Powershell in Action 1st Edition Bruce G. Payette
Windows Powershell in Action 1st Edition Bruce G. Payette
moudenongui20
 
Learn Powershell Scripting Tutorial Full Course 1dollarcart.com.pdf
Learn Powershell Scripting Tutorial Full Course 1dollarcart.com.pdfLearn Powershell Scripting Tutorial Full Course 1dollarcart.com.pdf
Learn Powershell Scripting Tutorial Full Course 1dollarcart.com.pdf
ClapperboardCinemaPV
 
Windows Powershell in Action 1st Edition Bruce G. Payette
Windows Powershell in Action 1st Edition Bruce G. PayetteWindows Powershell in Action 1st Edition Bruce G. Payette
Windows Powershell in Action 1st Edition Bruce G. Payette
zahithovor
 
One man loves powershell once he failed
One man loves powershell once he failedOne man loves powershell once he failed
One man loves powershell once he failed
Kazuhiro Matsushima
 
How To Create PowerShell Function Mandatory Parameter and Optional Parameter
How To Create PowerShell Function Mandatory Parameter and Optional ParameterHow To Create PowerShell Function Mandatory Parameter and Optional Parameter
How To Create PowerShell Function Mandatory Parameter and Optional Parameter
VCP Muthukrishna
 
Introduction To Power Shell
Introduction To Power ShellIntroduction To Power Shell
Introduction To Power Shell
Ivan Suhinin
 
How To Create PowerShell Function
How To Create PowerShell FunctionHow To Create PowerShell Function
How To Create PowerShell Function
VCP Muthukrishna
 
Advanced PowerShell Automation
Advanced PowerShell AutomationAdvanced PowerShell Automation
Advanced PowerShell Automation
kieranjacobsen
 
PowerShell 101
PowerShell 101PowerShell 101
PowerShell 101
Thomas Lee
 
Unix day3 v1.3
Unix day3 v1.3Unix day3 v1.3
Unix day3 v1.3
xavier john
 
How To Create Power Shell Function Mandatory Parameter Value
How To Create Power Shell Function Mandatory Parameter ValueHow To Create Power Shell Function Mandatory Parameter Value
How To Create Power Shell Function Mandatory Parameter Value
VCP Muthukrishna
 
learn ps new advance for all powershell.pptx
learn ps new advance for all powershell.pptxlearn ps new advance for all powershell.pptx
learn ps new advance for all powershell.pptx
Ronnie Kapoor
 
PowerShell Scripting and Modularization (TechMentor Fall 2011)
PowerShell Scripting and Modularization (TechMentor Fall 2011)PowerShell Scripting and Modularization (TechMentor Fall 2011)
PowerShell Scripting and Modularization (TechMentor Fall 2011)
Concentrated Technology
 
PowerShellForDBDevelopers
PowerShellForDBDevelopersPowerShellForDBDevelopers
PowerShellForDBDevelopers
Bryan Cafferky
 
Powershell notes
Powershell notesPowershell notes
Powershell notes
Carlos Amorim
 
Power shell training
Power shell trainingPower shell training
Power shell training
David Brabant
 
PowerShell Zero To Hero Workshop!
PowerShell Zero To Hero Workshop!PowerShell Zero To Hero Workshop!
PowerShell Zero To Hero Workshop!
Daisy Stevens
 
PowerShell Fundamentals
PowerShell FundamentalsPowerShell Fundamentals
PowerShell Fundamentals
mozdzen
 
PowerShell Core Skills (TechMentor Fall 2011)
PowerShell Core Skills (TechMentor Fall 2011)PowerShell Core Skills (TechMentor Fall 2011)
PowerShell Core Skills (TechMentor Fall 2011)
Concentrated Technology
 
Windows Powershell in Action 1st Edition Bruce G. Payette
Windows Powershell in Action 1st Edition Bruce G. PayetteWindows Powershell in Action 1st Edition Bruce G. Payette
Windows Powershell in Action 1st Edition Bruce G. Payette
takabedembe
 
Windows Powershell in Action 1st Edition Bruce G. Payette
Windows Powershell in Action 1st Edition Bruce G. PayetteWindows Powershell in Action 1st Edition Bruce G. Payette
Windows Powershell in Action 1st Edition Bruce G. Payette
moudenongui20
 
Learn Powershell Scripting Tutorial Full Course 1dollarcart.com.pdf
Learn Powershell Scripting Tutorial Full Course 1dollarcart.com.pdfLearn Powershell Scripting Tutorial Full Course 1dollarcart.com.pdf
Learn Powershell Scripting Tutorial Full Course 1dollarcart.com.pdf
ClapperboardCinemaPV
 
Windows Powershell in Action 1st Edition Bruce G. Payette
Windows Powershell in Action 1st Edition Bruce G. PayetteWindows Powershell in Action 1st Edition Bruce G. Payette
Windows Powershell in Action 1st Edition Bruce G. Payette
zahithovor
 
One man loves powershell once he failed
One man loves powershell once he failedOne man loves powershell once he failed
One man loves powershell once he failed
Kazuhiro Matsushima
 
How To Create PowerShell Function Mandatory Parameter and Optional Parameter
How To Create PowerShell Function Mandatory Parameter and Optional ParameterHow To Create PowerShell Function Mandatory Parameter and Optional Parameter
How To Create PowerShell Function Mandatory Parameter and Optional Parameter
VCP Muthukrishna
 
Introduction To Power Shell
Introduction To Power ShellIntroduction To Power Shell
Introduction To Power Shell
Ivan Suhinin
 
How To Create PowerShell Function
How To Create PowerShell FunctionHow To Create PowerShell Function
How To Create PowerShell Function
VCP Muthukrishna
 
Advanced PowerShell Automation
Advanced PowerShell AutomationAdvanced PowerShell Automation
Advanced PowerShell Automation
kieranjacobsen
 
PowerShell 101
PowerShell 101PowerShell 101
PowerShell 101
Thomas Lee
 
How To Create Power Shell Function Mandatory Parameter Value
How To Create Power Shell Function Mandatory Parameter ValueHow To Create Power Shell Function Mandatory Parameter Value
How To Create Power Shell Function Mandatory Parameter Value
VCP Muthukrishna
 
learn ps new advance for all powershell.pptx
learn ps new advance for all powershell.pptxlearn ps new advance for all powershell.pptx
learn ps new advance for all powershell.pptx
Ronnie Kapoor
 
PowerShell Scripting and Modularization (TechMentor Fall 2011)
PowerShell Scripting and Modularization (TechMentor Fall 2011)PowerShell Scripting and Modularization (TechMentor Fall 2011)
PowerShell Scripting and Modularization (TechMentor Fall 2011)
Concentrated Technology
 
PowerShellForDBDevelopers
PowerShellForDBDevelopersPowerShellForDBDevelopers
PowerShellForDBDevelopers
Bryan Cafferky
 
Ad

PowerShell Functions

  • 1. PowerShell Functions Presented by Mike Pfeiffer http://www.AZPoSH.com
  • 2. Agenda PowerShell Functions What is a PowerShell Function? Structure of a PowerShell Function Function Guidelines Input Parameters Output Scope Functions in the Pipeline Begin, Process and End Blocks Parameter Attributes Comment Based Help
  • 3. What is a PowerShell Function? Think of it as a block of code that behaves as a cmdlet Can be loaded from your profile, dot sourced via a script, or imported from a module Can be called or defined in another function or script
  • 4. Structure of a PowerShell Function
  • 5. Function Guidelines Must be defined before they are called Call functions without parenthesis or commas: Get-MyStuff –Param1 $value1 –Param2 $value2 Get-MyStuff $value1 $value2
  • 6. Input Parameters The $args variable Named and positional parameters Parameter type constraints Initializing parameters with default values The switch parameter
  • 7. Output Write-Host is for writing to the console Use Write-Output to return function output Don’t use the Return keyword to return output
  • 8. Scope Functions have internal scope Don’t use or modify variables from parent scope Consider using default values for internal variables
  • 9. Functions in the Pipeline The $input variable Filters
  • 10. Begin, Process and End Blocks
  • 11. Parameter Attributes Parameter Attribute Declaration (MSDN): http://bit.ly/bzGu34
  • 13. Resources Get-Help About_Functions Get-Help About_Functions_Advanced Get-Help About_Comment_Based_Help PowerShell in Action http://manning.com/payette2/ PowerGUI PowerShell v2 Snippets http://bit.ly/azDkJG