Table of Contents
In PowerShell, it is essential to check whether arguments exist, especially when creating scripts that take input parameters. However, before delving into the specifics of developing a PowerShell script that accepts arguments and checks for their presence, learning how to create a script .ps1
file within a directory is essential.
You need to go through the following steps to create a PowerShell script (yourFileName.ps1
file) within a directory:
- Open a text editor like Notepad, Notepad++, or Visual Studio Code. For example, I am using
Notepad
. - Write your PowerShell script in the text editor.
- Save the file in the directory you chose. For example, I have chosen the E:\Test1 directory.
Note: When saving the file, choose file typeAll files
. - Name your file with a
.ps1
extension.
Now you know about creating a PowerShell script file, it’s time to further expand your expertise in PowerShell by learning how to verify the existence of arguments.
Using $PSBoundParameters
with Param
block
Use the $PSBoundParameters
with Param
block to check if arguments exist in PowerShell.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Param( [string]$arg1, [string]$arg2 ) if ($PSBoundParameters.ContainsKey('arg1')) { Write-Host "The first argument is $($PSBoundParameters['arg1'])" } else { Write-Host "No arguments were passed for arg1" } if ($PSBoundParameters.ContainsKey('arg2')) { Write-Host "The second argument is $($PSBoundParameters['arg2'])" } else { Write-Host "No arguments were passed for arg2" } |
The above code fence is saved as Script2.ps1
in the directory E:\Test1. So, the complete file path is E:\Test1\Script2.ps1.
Now, run the following path with arguments on PowerShell:
1 2 3 |
E:\Test1\Script2.ps1 Hello World |
1 2 3 4 |
The first argument is Hello The second argument is World |
The above code fence is similar to the previous code block, but it uses the $PSBoundParameters
automatic variable that contains a dictionary of key-value pairs for the current function or script block. Each key in the dictionary represents the name of a parameter that has been passed to the function or script block, and the corresponding value is the argument that was passed for that parameter.
After defining the parameters, the script checks if the $PSBoundParameters
automatic variable contains a key named arg1
or arg2
using the ContainsKey()
method. This method returns true if the key exists in the dictionary and false otherwise.
If the $PSBoundParameters
dictionary contains the arg1
key, the script uses string interpolation ($()
) to display the value of $arg1
in a console message. Otherwise, it displays a message indicating that no arguments were passed for $arg1
. Similarly, it works for the $arg2
and displays the value of arg2
to the console.
Using if with Param
block
Use if statement with the Param
block to check if arguments exist in PowerShell.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Param( [string]$arg1, [string]$arg2 ) if($arg1){ Write-Host "The first argument is $arg1" } else{ Write-Host "No arguments were passed for arg1" } if($arg2){ Write-Host "The second argument is $arg2" } else{ Write-Host "No arguments were passed for arg2" } |
The above code fence is saved as Script1.ps1
in the directory E:\Test1. So, the complete file path is E:\Test1\Script1.ps1.
Now, run the following path with arguments on PowerShell:
1 2 3 |
E:\Test1\Script1.ps1 Hello World |
1 2 3 4 |
The first argument is Hello The second argument is World |
In the above code, the Param()
block at the beginning of the script defines the parameters and their types as strings. The script then checks if $arg1
has been passed as an argument. If it has, it outputs a message stating, The first argument is [value of arg1]
.
It outputs a message describing No arguments were passed for $arg1
if it has not been passed. The script then does the same for $arg2
, checking if it has been passed and outputs the appropriate message. Finally, the Write-Host
cmdlet outputs the messages to the console.
Further reading:
Using args
Variable
Use the args
variable to check if arguments exist in PowerShell.
1 2 3 4 5 6 7 8 |
if($args.Length -gt 0){ Write-Host "The argument is: $args" } else{ Write-Host "No arguments were passed" } |
The above code fence is saved as Script3.ps1
in the directory E:\Test1. So, the entire file path is E:\Test1\Script3.ps1.
Now, run the following path with arguments on PowerShell:
1 2 3 |
E:\Test1\Script3.ps1 Hello |
1 2 3 |
The argument is: Hello |
The script accepts one argument in the above code and uses the $args
automatic variable to check if an argument was passed. When we run the script with an argument E:\Test1\Script3.ps1 Hello, the argument value is displayed as Hello
. And, if we run the Script3.ps1
file without specifying the arguments, it will execute the Write-Host
cmdlet within the else
block, saying No arguments were passed
.
Using these three methods to check if arguments exist in PowerShell can help improve the reliability and flexibility of your scripts and functions whether you choose to use the $args
automatic variable, if statement with Param()
block, or $PSBoundParameters
with Param
block depending on the specific needs of your script or function.
Using these methods, you can ensure that your script or function can handle different scenarios and provide an accurate output based on the presence or absence of arguments.
That’s all about how to check if argument exists in PowerShell.