user983965
user983965

Reputation: 1121

Parameter equals none if not specified powershell

I am trying to pass various parameters to a PowerShell script, however if no value is passed that script exits with an error. For example:

.powershellscript.ps1 -path C:\test -Name text.txt -Date 20111212

Works fine, however if I execute:

.powershellscript.ps1 -path C:\test -Name text.txt -Date

I receive the following error

"Missing an argument for parameter 'Date'. Specify a parameter of type 'System.Object' and try again."

I want to be able to assign the Date value to $Null if it's not passed through the script.

Inside the .powershellscript.ps1, the parameters is handled as:

param(
    $path,
    $Name,
    $Date
)

Any help would be greatly appreciated.

Upvotes: 0

Views: 4726

Answers (1)

MrKWatkins
MrKWatkins

Reputation: 2668

If you don't want to pass a value for $Date call the script like this:

.powershellscript.ps1 -path C:\test -Name text.txt

$Date should then be $null in your script.

Upvotes: 2

Related Questions