How to determine "stranger" argument, passed to PS script

Hi big and all-knowing ALL.
I have PS script that uses named parameters. Something like:

param (
function Usage
    write-host "Usage:"
    exit 1
if ($help){ Usage }

If I invoke it like:
myScript.ps1 --help

everything works fine - I get usage.
If I invoke it without parameters - it also is Ok - I get nothing.
Problem occurs when I invoke it with total wrong parameter. For example :

myScript.ps1 --kuku
myScript.ps1 kuku

In this case “param” section simply ignore parameter, that does not suit its definitions.
Of cause I can abandon “param” section and switch to primitive loop over @args array and manual parsing of arguments. This way I can for sure discover that user entered unsupported argument.
But question is - do PS have more elegant way to do such validation?
I believe - it have to have something that do it, since problem seems to be fairly common. But I can not find anything that suite this task… :anguished:

Try adding:


At the beginning of your script.

1 Like

Hi, welcome to the forum :wave:

As well as [CmdletBinding()], for the specific purpose of providing help information, look into about Comment Based Help - PowerShell | Microsoft Docs

This will allow you to take full advantage of the PowerShell help system which, for very little extra effort, gives you a lot of nice features.

1 Like

10x - will try it.
Looks promising

Hi Tony
I have used [CmdletBinding()] and it generally do what I need.
But I face 2 problems - that I do not success to solve.

  1. If I pass wrong parameter - PS throws error message about it (as expected ) and stops execution of script. Good! But I did not success to find a way to customize this error message. Is it possible?

  2. In case of wrong parameter - exit code of script is - 0. Can I change it somehow to be for example - 1? So - caller script/batch will “know” that something gone wrong?

Do some one have any idea about how to customize CmdletBinding error message about wrong parameter, and exit code that is thrown from script?

I would encourage you to investigate the link on Comment Based Help posted by matt-bloomfield. There really is no reason to write code for help when that functionality is built into PowerShell.

Hi Darwin. Generally I agree with you.
But I have pre-defined requirement, that in case of wrong command-line parameters script will exit with exit code 1.
And default behavior, pre-scribed by use of CmdletBinding - exit code is - 0.
Although - you get err-message about wrong parameter, but exit code of script is - 0

You’re calling the ps1 file? Please show us exactly how you’re invoking, formatted as code please

Hi krzydoug

powershell.exe -ExecutionPolicy Bypass -File check_java_ready.ps1 -java_path c:\kuku_java

In this case your powershell.exe command completes successfully, so the exit code would be 0. If it has an error itself it will return an error. I remember reading something about this but can’t find it at the moment. In the meantime, I’d check $lastexitcode it code in your script itself.

Check Michael’s answer here Returning an exit code from a PowerShell script - Stack Overflow