Powershell how to count the number of arguments

Writing a powershell script .ps1 file, and I was wondering how I would find how many arguments the user passed. For example, I need my program to process a string in multiple files. Therefore, the user should be able to enter

./Script "a" ex1.txt or

./Script "a" ex1.txt ex2.txt

I know that in bash we can use $# But how can I check powershell. Thanks

+7
scripting powershell arguments
source share
3 answers

In a script or function, you can access the arguments passed through the $args automatically populated variable. $args is an array, so you can find the number of arguments passed through its Count property.

Example:

 function PrintArgs() { Write-Host "You passed $($args.Count) arguments:" $args | Write-Host } 

Call PrintArgs :

 > PrintArgs 'a' 'b' 'c' You passed 3 arguments: a b c 
+11
source share

Read some help files if you have time; they are usually very useful!

 get-help * get-help *parameters* get-help about_Parameters get-help about_Automatic_Variables get-help about_Functions* #etc... 

Help notes to help you:

$ Args

Contains an array of undeclared parameters and / or parameter values ​​that are passed to a function, script or script. When you create a function, you can declare the parameters using param or by adding a comma-separated list of parameters in brackets after the function name.

In an event action, the $ Args variable contains objects that represent the event arguments of the event being processed. This variable is populated only in the action event log block of the command. The value of this variable can also be found in the SourceArgs property of the PSEventArgs object (System.Management.Automation.PSEventArgs) that returns the Get-Event.

$ PSBoundParameters

Contains a dictionary of parameters that are passed to the script or function and their current values. This variable only matters in the area where the parameters are declared, such as a script or function. You can use it to display or change the current parameter values ​​or pass parameter values ​​to another script function or.

If you finish using both related parameters and unrelated arguments, you can combine the two ( example )

Slightly off topic:

I highly recommend not using $ args. You should always use related parameters, there are very few cases when you are forced not to use them.

Why? All functions that take parameters should follow best practices and use the param () block for your parameters. If you read the parameters in the help files, you will find that using the parameter block gives you all sorts of useful functions, such as validation, force parameters, accepting pipeline input, etc. This is also necessary before allowing PowerShell to do even more work for you with [ cmdletbinding ()]

Hooray!

+6
source share

For this, the built-in $Args and the Count property are used.

 $Args.Count 

This will give you the number of arguments the user passed to the script.

+4
source share

All Articles