How to setup powershell parameters?

NVIT
NVIT used Ask the Experts™
How to setup powershell parameters?

I'd like the command to do this. Basically,
1) Copy or Delete is used but not both.
2) DaysOld is required with both.

Example:

.\command.ps1 -DaysOld 30 -copy "c:\folder"

Open in new window


...or:

.\command.ps1 -DaysOld 30 -delete "c:\folder"

Open in new window


This is what I have. It kind of works. I don't understand what I'm doing. Is there is a better or correct way to do it:

[CmdletBinding(DefaultParameterSetName='Copy')]
    Param(
        [Parameter(ParameterSetName='Copy',Mandatory=$false,Position=0)]
        [Parameter(ParameterSetName='DaysOld',Mandatory=$false,Position=0)]
        [String]$Copy,

        [Parameter(ParameterSetName='Delete',Mandatory=$false,Position=0)]
        [Parameter(ParameterSetName='DaysOld',Mandatory=$false,Position=0)]
        [String]$Delete,

        [Parameter(ParameterSetName='DaysOld',Mandatory=$false,Position=0)]
        [Int]$DaysOld
    )

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Since $DaysOld can be used in all ParameterSets, and only in conjunction with either $Copy or $Delete, you don't need to define a third one.
And position 0 for DaysOld is already used.
[CmdletBinding(DefaultParameterSetName='Copy')]
	Param(
		[Parameter(Mandatory=$false, Position=0, ParameterSetName='Copy')]
		[String]$Copy,

		[Parameter(Mandatory=$false, Position=0, ParameterSetName='Delete')]
		[String]$Delete,

		[Parameter(Mandatory=$false, Position=1)]
		[Int]$DaysOld
	)

Open in new window

NVITEnd-user support

Author

Commented:
Thanks for the quick response. Is there a way to make DaysOld mandatory? If not, no biggie. I can test for it in the function.
NVITEnd-user support

Author

Commented:
never mind. Sorry. Just change it to $true. just found out.
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

NVITEnd-user support

Author

Commented:
I should have said: I there a way to make EITHER Copy or Delete mandatory? But not both.
Most Valuable Expert 2018
Distinguished Expert 2018
Just make both mandatory. Since they're in different sets, only one will actually be mandatory
[CmdletBinding(DefaultParameterSetName='Copy')]
	Param(
		[Parameter(Mandatory=$true, Position=0, ParameterSetName='Copy')]
		[String]$Copy,

		[Parameter(Mandatory=$true, Position=0, ParameterSetName='Delete')]
		[String]$Delete,

		[Parameter(Mandatory=$true, Position=1)]
		[Int]$DaysOld
	)

Open in new window

NVITEnd-user support

Author

Commented:
Awesome. Thanks again!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial