SlideShare a Scribd company logo
Windows PowerShell Basics – Function Mandatory 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.........................................................................................................................................2
Invoke Function – Attribute Name ...........................................................................................................2
PowerShell Output #1...........................................................................................................................2
Invoke Function – Without Passing Argument Value ...............................................................................2
Invoke Function – Passing Argument Value Space ...................................................................................3
PowerShell Output #2...........................................................................................................................3
Invoke Function – Passing Argument Value..............................................................................................3
PowerShell Output #3...........................................................................................................................3
Invoke Function – Passing Argument Value..............................................................................................3
PowerShell Output #4...........................................................................................................................3
Windows PowerShell Basics – Function Mandatory 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. When the value is not given,
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 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, “Parameter” is set to true and for the argument position 1, which will ensure that the
argument value is mandatory.
#
# Simple Function with one Mandatory Argument Value
#
function MandatoryParameter
{
param(
[Parameter(Mandatory=$True, Position=1)]
[ValidateNotNullOrEmpty()]
Windows PowerShell Basics – Function Mandatory Argument
2 | P a g e
[string]$Parameter_Position_One
)
Clear-Host
Write-Host "`nParameter Value that was passed : $Parameter_Position_One`n"
[console]::beep(500,300)
}
Invoke Function
To invoke / call the function, call the function from the PowerShell CLI or PowerShell ISE.
MandatoryParameter 007
Invoke Function – Attribute Name
Invoke the function and pass value for the specific attribute.
MandatoryParameter -Parameter_Position_One 007
PowerShell Output #1
When the function is called / executed with parameter value being passed; below output will be displayed.
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;
Windows PowerShell Basics – Function Mandatory Argument
3 | P a g e
Invoke Function – Passing Argument Value Space
When you intend to set the argument value with multiple words, enclose value with double quotes after
invoking the function.
PowerShell Output #2
Invoke Function – Passing Argument Value
When you intend to run the script is run with single quote;
MandatoryParameter 'Function Parameter Passing Mandatory Argument Value'
PowerShell Output #3
Invoke Function – Passing Argument Value
When you intend to run the script along with the against each parameter name its value;
MandatoryParameter –Parameter_Position_One Parameter
PowerShell Output #4

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
Bash Script - How To Monitor Application Error Logs and Send Notification
VCP Muthukrishna
 
PDF
How To Connect to Active Directory User Validation
VCP Muthukrishna
 
PDF
How To Create EBS Snapshot and Restore EBS Snapshot – Linux Instance
VCP Muthukrishna
 
PDF
How To Install and Configure SNMP on RHEL 7 or CentOS 7
VCP Muthukrishna
 
DOCX
How to Configure Amazon AWS EC2 Elastic IP Address
VCP Muthukrishna
 
PDF
How To Create EC2 instance Linux Server
VCP Muthukrishna
 
PDF
Nginx bind() to 0.0.0.0:9080 failed
VCP Muthukrishna
 
PDF
How to Install and Configure Cacti on Linux
VCP Muthukrishna
 
PPTX
psCloudstack Internals
Hans van Veen
 
PDF
How To Manage Linux User on RHEL 7
VCP Muthukrishna
 
PDF
How To Install and Configure AWS CLI for Windows
VCP Muthukrishna
 
PDF
How To Configure Amazon EC2 Security Groups
VCP Muthukrishna
 
PDF
Install and Configure WordPress in AWS on RHEL 7 or CentOS 7
VCP Muthukrishna
 
PDF
How To Install and Configure Apache SSL on CentOS 7
VCP Muthukrishna
 
PDF
How to Troubleshoot SELinux Audit2Allow unable to open (null)
VCP Muthukrishna
 
DOCX
How to configure amazon ec2 load balancer
VCP Muthukrishna
 
PDF
How To Install and Use ABRT CLI on RHEL 7
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
 
Bash Script - How To Monitor Application Error Logs and Send Notification
VCP Muthukrishna
 
How To Connect to Active Directory User Validation
VCP Muthukrishna
 
How To Create EBS Snapshot and Restore EBS Snapshot – Linux Instance
VCP Muthukrishna
 
How To Install and Configure SNMP on RHEL 7 or CentOS 7
VCP Muthukrishna
 
How to Configure Amazon AWS EC2 Elastic IP Address
VCP Muthukrishna
 
How To Create EC2 instance Linux Server
VCP Muthukrishna
 
Nginx bind() to 0.0.0.0:9080 failed
VCP Muthukrishna
 
How to Install and Configure Cacti on Linux
VCP Muthukrishna
 
psCloudstack Internals
Hans van Veen
 
How To Manage Linux User on RHEL 7
VCP Muthukrishna
 
How To Install and Configure AWS CLI for Windows
VCP Muthukrishna
 
How To Configure Amazon EC2 Security Groups
VCP Muthukrishna
 
Install and Configure WordPress in AWS on RHEL 7 or CentOS 7
VCP Muthukrishna
 
How To Install and Configure Apache SSL on CentOS 7
VCP Muthukrishna
 
How to Troubleshoot SELinux Audit2Allow unable to open (null)
VCP Muthukrishna
 
How to configure amazon ec2 load balancer
VCP Muthukrishna
 
How To Install and Use ABRT CLI on RHEL 7
VCP Muthukrishna
 

More from VCP Muthukrishna (18)

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
How To Check and Delete a File via PowerShell
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 Create PowerShell Function
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
 
How To Check and Delete a File via PowerShell
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 Create PowerShell Function
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)

PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Biography of Daniel Podor.pdf
Daniel Podor
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Ad

How To Create Power Shell Function Mandatory Parameter Value

  • 1. Windows PowerShell Basics – Function Mandatory 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.........................................................................................................................................2 Invoke Function – Attribute Name ...........................................................................................................2 PowerShell Output #1...........................................................................................................................2 Invoke Function – Without Passing Argument Value ...............................................................................2 Invoke Function – Passing Argument Value Space ...................................................................................3 PowerShell Output #2...........................................................................................................................3 Invoke Function – Passing Argument Value..............................................................................................3 PowerShell Output #3...........................................................................................................................3 Invoke Function – Passing Argument Value..............................................................................................3 PowerShell Output #4...........................................................................................................................3
  • 2. Windows PowerShell Basics – Function Mandatory 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. When the value is not given, 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 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, “Parameter” is set to true and for the argument position 1, which will ensure that the argument value is mandatory. # # Simple Function with one Mandatory Argument Value # function MandatoryParameter { param( [Parameter(Mandatory=$True, Position=1)] [ValidateNotNullOrEmpty()]
  • 3. Windows PowerShell Basics – Function Mandatory Argument 2 | P a g e [string]$Parameter_Position_One ) Clear-Host Write-Host "`nParameter Value that was passed : $Parameter_Position_One`n" [console]::beep(500,300) } Invoke Function To invoke / call the function, call the function from the PowerShell CLI or PowerShell ISE. MandatoryParameter 007 Invoke Function – Attribute Name Invoke the function and pass value for the specific attribute. MandatoryParameter -Parameter_Position_One 007 PowerShell Output #1 When the function is called / executed with parameter value being passed; below output will be displayed. 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;
  • 4. Windows PowerShell Basics – Function Mandatory Argument 3 | P a g e Invoke Function – Passing Argument Value Space When you intend to set the argument value with multiple words, enclose value with double quotes after invoking the function. PowerShell Output #2 Invoke Function – Passing Argument Value When you intend to run the script is run with single quote; MandatoryParameter 'Function Parameter Passing Mandatory Argument Value' PowerShell Output #3 Invoke Function – Passing Argument Value When you intend to run the script along with the against each parameter name its value; MandatoryParameter –Parameter_Position_One Parameter PowerShell Output #4