SlideShare a Scribd company logo
Windows PowerShell Basics – Function Mandatory and
Optional Argument
i | P a g e
Table of Contents
Overview.......................................................................................................................................................1
Applies To..................................................................................................................................................1
Pre-Requisites ...........................................................................................................................................1
PowerShell Script – Function ........................................................................................................................1
Code Snippet.............................................................................................................................................1
Invoke Function – Attribute Name ...........................................................................................................2
PowerShell Output #1...........................................................................................................................2
Invoke Function – Without Passing Argument Value ...............................................................................3
PowerShell Output #2...........................................................................................................................3
Windows PowerShell Basics – Function Mandatory and
Optional Argument
1 | P a g e
Overview
PowerShell functions are helpful in saving time when you have repetitive task(s) to be executed. In this
guide we will demonstrate, passing function argument(s) value as mandatory and an optional argument.
When the value is not given for the mandatory, function will prompt for parameter value.
Applies To
Tested on Windows 10, Windows 2008 R2 and Windows 2012.
Pre-Requisites
Launch PowerShell Command Console or PowerShell ISE.
To run this script, Execution Policy should be set to either of these “AllSigned” or “RemoteSigned” or
“Unrestricted”, you can get current execution policy by running the command; “Get-ExecutionPolicy”.
Each Policy type and its purpose is shown in the below table.
Policy Type Purpose
Restricted No scripts can be run. Windows PowerShell can be used only in interactive mode.
AllSigned Only scripts signed by a trusted publisher can be run.
RemoteSigned Downloaded scripts must be signed by a trusted publisher before they can be run.
Unrestricted No restrictions; all Windows PowerShell scripts can be run.
PowerShell Script – Function
This PowerShell script call function with function’s mandatory argument / parameter value, when the
argument value is passed; Passed value will be displayed else an exception will be thrown.
Code Snippet
In this function, one “Parameter” is set to false and “Parameter” is set to true; for argument position 1 is
optional and position 2 argument is mandatory.
#
# Simple Function With One Argument Mandatory Value and Second Argument Optional
#
function MandatoryParameter_And_OptionalParameter
{
param(
Windows PowerShell Basics – Function Mandatory and
Optional Argument
2 | P a g e
[Parameter(Mandatory=$False, Position=1)]
[string]$Parameter_Position_One,
[Parameter(Mandatory=$True, Position=2)]
[ValidateNotNull()]
[string]$Parameter_Position_Two
)
Clear-Host
Write-Host "`n1st Parameter Value that was passed : $Parameter_Position_One`n"
Write-Host "`n2nd Parameter Value that was passed : $Parameter_Position_Two`n"
[console]::beep(500, 300)
}
Invoke Function – Attribute Name
To invoke / call the function, call the function from the PowerShell CLI or PowerShell ISE. Invoke the
function and pass value for the specific attribute.
MandatoryParameter_And_OptionalParameter -Parameter_Position_Two Value_Passed
PowerShell Output #1
After execution of the function and mandatory argument value is passed; argument value is displayed as
below;
Windows PowerShell Basics – Function Mandatory and
Optional Argument
3 | P a g e
Invoke Function – Without Passing Argument Value
When the parameter/argument value is not passed; script will ask for input of argument value, as shown
below;
MandatoryParameter_And_OptionalParameter
PowerShell Output #2

More Related Content

What's hot (20)

PDF
Install and Configure RSyslog – CentOS 7 / RHEL 7
VCP Muthukrishna
 
PDF
How To View Current Execution Policy PowerShell
VCP Muthukrishna
 
PDF
How To Install and Configure Open SSH Server on Ubuntu
VCP Muthukrishna
 
PDF
How To Connect to Active Directory User Validation
VCP Muthukrishna
 
PDF
Bash Script - How To Monitor Application Error Logs and Send Notification
VCP Muthukrishna
 
PDF
How To Install and Configure SNMP on RHEL 7 or CentOS 7
VCP Muthukrishna
 
PDF
How To Create EBS Snapshot and Restore EBS Snapshot – Linux Instance
VCP Muthukrishna
 
PPTX
psCloudstack Internals
Hans van Veen
 
PDF
How To Manage Linux User on RHEL 7
VCP Muthukrishna
 
PDF
Nginx bind() to 0.0.0.0:9080 failed
VCP Muthukrishna
 
PDF
How To Configure Amazon EC2 Security Groups
VCP Muthukrishna
 
PDF
How to Install and Configure Cacti on Linux
VCP Muthukrishna
 
PDF
How To Install and Configure Apache SSL on CentOS 7
VCP Muthukrishna
 
PDF
How To Install and Configure AWS CLI for Windows
VCP Muthukrishna
 
PDF
Install and Configure WordPress in AWS on RHEL 7 or CentOS 7
VCP Muthukrishna
 
ODP
Specs Presentation
Synesso
 
DOCX
How to configure amazon ec2 load balancer
VCP Muthukrishna
 
PDF
How To Install and Use ABRT CLI on RHEL 7
VCP Muthukrishna
 
PDF
How To Audit Server Login and Shutdown or Reboot Activity
VCP Muthukrishna
 
PDF
How to Troubleshoot SELinux Audit2Allow unable to open (null)
VCP Muthukrishna
 
Install and Configure RSyslog – CentOS 7 / RHEL 7
VCP Muthukrishna
 
How To View Current Execution Policy PowerShell
VCP Muthukrishna
 
How To Install and Configure Open SSH Server on Ubuntu
VCP Muthukrishna
 
How To Connect to Active Directory User Validation
VCP Muthukrishna
 
Bash Script - How To Monitor Application Error Logs and Send Notification
VCP Muthukrishna
 
How To Install and Configure SNMP on RHEL 7 or CentOS 7
VCP Muthukrishna
 
How To Create EBS Snapshot and Restore EBS Snapshot – Linux Instance
VCP Muthukrishna
 
psCloudstack Internals
Hans van Veen
 
How To Manage Linux User on RHEL 7
VCP Muthukrishna
 
Nginx bind() to 0.0.0.0:9080 failed
VCP Muthukrishna
 
How To Configure Amazon EC2 Security Groups
VCP Muthukrishna
 
How to Install and Configure Cacti on Linux
VCP Muthukrishna
 
How To Install and Configure Apache SSL on CentOS 7
VCP Muthukrishna
 
How To Install and Configure AWS CLI for Windows
VCP Muthukrishna
 
Install and Configure WordPress in AWS on RHEL 7 or CentOS 7
VCP Muthukrishna
 
Specs Presentation
Synesso
 
How to configure amazon ec2 load balancer
VCP Muthukrishna
 
How To Install and Use ABRT CLI on RHEL 7
VCP Muthukrishna
 
How To Audit Server Login and Shutdown or Reboot Activity
VCP Muthukrishna
 
How to Troubleshoot SELinux Audit2Allow unable to open (null)
VCP Muthukrishna
 

Similar to How To Create PowerShell Function Mandatory Parameter and Optional Parameter (20)

ODT
Testing in-python-and-pytest-framework
Arulalan T
 
PDF
How To Create PowerShell Function
VCP Muthukrishna
 
PPTX
PowerShell Fundamentals
mozdzen
 
PPT
Qtp92 Presentation
a34sharm
 
PDF
Java script tutorial
Doeun KOCH
 
PDF
Declarative presentations UIKonf
Nataliya Patsovska
 
PDF
Pandora FMS: Outlook Anywhere Plugin
Pandora FMS
 
PDF
MyShell - English
Johnnatan Messias
 
PPTX
Python_UNIT-I.pptx
mustafatahertotanawa1
 
PDF
Apex 5 plugins for everyone version 2018
Alan Arentsen
 
PDF
Workflow as code with Azure Durable Functions
Massimo Bonanni
 
PPS
About Qtp 92
techgajanan
 
PPS
About Qtp_1 92
techgajanan
 
PPS
About QTP 9.2
chandrasekhar
 
PDF
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
PDF
How To Check and Delete a File via PowerShell
VCP Muthukrishna
 
PPTX
What is Puppet? | How Puppet Works? | Puppet Tutorial For Beginners | DevOps ...
Simplilearn
 
PPTX
Powershell Tech Ed2009
rsnarayanan
 
PDF
Parameter Estimation User Guide
Andy Salmon
 
PPT
Understanding Framework Architecture using Eclipse
anshunjain
 
Testing in-python-and-pytest-framework
Arulalan T
 
How To Create PowerShell Function
VCP Muthukrishna
 
PowerShell Fundamentals
mozdzen
 
Qtp92 Presentation
a34sharm
 
Java script tutorial
Doeun KOCH
 
Declarative presentations UIKonf
Nataliya Patsovska
 
Pandora FMS: Outlook Anywhere Plugin
Pandora FMS
 
MyShell - English
Johnnatan Messias
 
Python_UNIT-I.pptx
mustafatahertotanawa1
 
Apex 5 plugins for everyone version 2018
Alan Arentsen
 
Workflow as code with Azure Durable Functions
Massimo Bonanni
 
About Qtp 92
techgajanan
 
About Qtp_1 92
techgajanan
 
About QTP 9.2
chandrasekhar
 
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
How To Check and Delete a File via PowerShell
VCP Muthukrishna
 
What is Puppet? | How Puppet Works? | Puppet Tutorial For Beginners | DevOps ...
Simplilearn
 
Powershell Tech Ed2009
rsnarayanan
 
Parameter Estimation User Guide
Andy Salmon
 
Understanding Framework Architecture using Eclipse
anshunjain
 
Ad

More from VCP Muthukrishna (16)

PDF
How to Fix Duplicate Packages in YUM on CentOS 7
VCP Muthukrishna
 
PDF
How To Install and Configure GNome on CentOS 7
VCP Muthukrishna
 
PDF
How To Connect To Active Directory PowerShell
VCP Muthukrishna
 
PDF
How To List Files on Remote Server - PowerShell
VCP Muthukrishna
 
PDF
How To List Files and Display In HTML Format
VCP Muthukrishna
 
PDF
Zimbra Troubleshooting - Mails not being Delivered or Deferred or Connection ...
VCP Muthukrishna
 
PDF
How To Setup SSH Keys on CentOS 7
VCP Muthukrishna
 
PDF
How To List Nginx Modules Installed / Complied on CentOS 7
VCP Muthukrishna
 
PDF
How To Configure Nginx Load Balancer on CentOS 7
VCP Muthukrishna
 
PDF
How To Install and Configure Screen on CentOS 7
VCP Muthukrishna
 
PDF
How To Install and Configure Salt Master on Ubuntu
VCP Muthukrishna
 
PDF
How To Protect SSH Access with Fail2Ban on RHEL 7
VCP Muthukrishna
 
PDF
How To Configure SNMP Logging on RHEL 7
VCP Muthukrishna
 
PDF
How To Find Package Installation Date on RHEL 7
VCP Muthukrishna
 
PDF
How to Upgrade Openfire on CentOS 7
VCP Muthukrishna
 
PDF
How To Reset root Password on CentOS 7
VCP Muthukrishna
 
How to Fix Duplicate Packages in YUM on CentOS 7
VCP Muthukrishna
 
How To Install and Configure GNome on CentOS 7
VCP Muthukrishna
 
How To Connect To Active Directory PowerShell
VCP Muthukrishna
 
How To List Files on Remote Server - PowerShell
VCP Muthukrishna
 
How To List Files and Display In HTML Format
VCP Muthukrishna
 
Zimbra Troubleshooting - Mails not being Delivered or Deferred or Connection ...
VCP Muthukrishna
 
How To Setup SSH Keys on CentOS 7
VCP Muthukrishna
 
How To List Nginx Modules Installed / Complied on CentOS 7
VCP Muthukrishna
 
How To Configure Nginx Load Balancer on CentOS 7
VCP Muthukrishna
 
How To Install and Configure Screen on CentOS 7
VCP Muthukrishna
 
How To Install and Configure Salt Master on Ubuntu
VCP Muthukrishna
 
How To Protect SSH Access with Fail2Ban on RHEL 7
VCP Muthukrishna
 
How To Configure SNMP Logging on RHEL 7
VCP Muthukrishna
 
How To Find Package Installation Date on RHEL 7
VCP Muthukrishna
 
How to Upgrade Openfire on CentOS 7
VCP Muthukrishna
 
How To Reset root Password on CentOS 7
VCP Muthukrishna
 
Ad

Recently uploaded (20)

PPSX
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
PDF
Simplify Your FME Flow Setup: Fault-Tolerant Deployment Made Easy with Packer...
Safe Software
 
PDF
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
PDF
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
PDF
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
PDF
Plugging AI into everything: Model Context Protocol Simplified.pdf
Abati Adewale
 
PDF
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
PDF
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
PPTX
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PDF
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
PDF
TrustArc Webinar - Navigating APAC Data Privacy Laws: Compliance & Challenges
TrustArc
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
PPTX
Smart Factory Monitoring IIoT in Machine and Production Operations.pptx
Rejig Digital
 
PDF
How to Comply With Saudi Arabia’s National Cybersecurity Regulations.pdf
Bluechip Advanced Technologies
 
PDF
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
PDF
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
PDF
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
PDF
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
PDF
DoS Attack vs DDoS Attack_ The Silent Wars of the Internet.pdf
CyberPro Magazine
 
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
Simplify Your FME Flow Setup: Fault-Tolerant Deployment Made Easy with Packer...
Safe Software
 
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
Plugging AI into everything: Model Context Protocol Simplified.pdf
Abati Adewale
 
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
TrustArc Webinar - Navigating APAC Data Privacy Laws: Compliance & Challenges
TrustArc
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
Smart Factory Monitoring IIoT in Machine and Production Operations.pptx
Rejig Digital
 
How to Comply With Saudi Arabia’s National Cybersecurity Regulations.pdf
Bluechip Advanced Technologies
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
DoS Attack vs DDoS Attack_ The Silent Wars of the Internet.pdf
CyberPro Magazine
 

How To Create PowerShell Function Mandatory Parameter and Optional Parameter

  • 1. Windows PowerShell Basics – Function Mandatory and Optional Argument i | P a g e Table of Contents Overview.......................................................................................................................................................1 Applies To..................................................................................................................................................1 Pre-Requisites ...........................................................................................................................................1 PowerShell Script – Function ........................................................................................................................1 Code Snippet.............................................................................................................................................1 Invoke Function – Attribute Name ...........................................................................................................2 PowerShell Output #1...........................................................................................................................2 Invoke Function – Without Passing Argument Value ...............................................................................3 PowerShell Output #2...........................................................................................................................3
  • 2. Windows PowerShell Basics – Function Mandatory and Optional Argument 1 | P a g e Overview PowerShell functions are helpful in saving time when you have repetitive task(s) to be executed. In this guide we will demonstrate, passing function argument(s) value as mandatory and an optional argument. When the value is not given for the mandatory, function will prompt for parameter value. Applies To Tested on Windows 10, Windows 2008 R2 and Windows 2012. Pre-Requisites Launch PowerShell Command Console or PowerShell ISE. To run this script, Execution Policy should be set to either of these “AllSigned” or “RemoteSigned” or “Unrestricted”, you can get current execution policy by running the command; “Get-ExecutionPolicy”. Each Policy type and its purpose is shown in the below table. Policy Type Purpose Restricted No scripts can be run. Windows PowerShell can be used only in interactive mode. AllSigned Only scripts signed by a trusted publisher can be run. RemoteSigned Downloaded scripts must be signed by a trusted publisher before they can be run. Unrestricted No restrictions; all Windows PowerShell scripts can be run. PowerShell Script – Function This PowerShell script call function with function’s mandatory argument / parameter value, when the argument value is passed; Passed value will be displayed else an exception will be thrown. Code Snippet In this function, one “Parameter” is set to false and “Parameter” is set to true; for argument position 1 is optional and position 2 argument is mandatory. # # Simple Function With One Argument Mandatory Value and Second Argument Optional # function MandatoryParameter_And_OptionalParameter { param(
  • 3. Windows PowerShell Basics – Function Mandatory and Optional Argument 2 | P a g e [Parameter(Mandatory=$False, Position=1)] [string]$Parameter_Position_One, [Parameter(Mandatory=$True, Position=2)] [ValidateNotNull()] [string]$Parameter_Position_Two ) Clear-Host Write-Host "`n1st Parameter Value that was passed : $Parameter_Position_One`n" Write-Host "`n2nd Parameter Value that was passed : $Parameter_Position_Two`n" [console]::beep(500, 300) } Invoke Function – Attribute Name To invoke / call the function, call the function from the PowerShell CLI or PowerShell ISE. Invoke the function and pass value for the specific attribute. MandatoryParameter_And_OptionalParameter -Parameter_Position_Two Value_Passed PowerShell Output #1 After execution of the function and mandatory argument value is passed; argument value is displayed as below;
  • 4. Windows PowerShell Basics – Function Mandatory and Optional Argument 3 | P a g e Invoke Function – Without Passing Argument Value When the parameter/argument value is not passed; script will ask for input of argument value, as shown below; MandatoryParameter_And_OptionalParameter PowerShell Output #2