0

I'm trying to execute a below Powershell command to create a new address list on exchange server with parameters like Name, Container, etc.

Container is an optional input/parameter, how do I omit it from cmdlet if its value is not provided?

I tried with IF conditionals but but does not seems working. Any help here?

New-AddressList -Name -Container \test MyAddressList5 -ConditionalStateOrProvince maha -IncludedRecipients MailboxUsers
1
  • We can't advise how to not include it if we don't know how are you getting the input? You might want to include a bit more code to show context :) Commented Oct 1, 2018 at 8:57

3 Answers 3

2

You can pass needed parameters with their corresponding values via hashtable. Add If/Else conditions to include properties. Like so:

$Container  = '\test MyAddressList5'

$Parameters = @{}
$Parameters.Add('ConditionalStateOrProvince','maha')
$Parameters.Add('IncludedRecipients','MailboxUsers')
if($Container){$Parameters.Add('Container',$Container)}

New-AddressList @Parameters

Also, when you need to include Switch parameter just pass $True. Like so:

$Parameters.Add('SomeSwitchParameter',$True)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, Hashtable. I have used Hashtable and the purpose is served. Thank you.
0

Have a look at the documentation for the -Container parameter in New-AddressList: https://learn.microsoft.com/en-us/powershell/module/exchange/email-addresses-and-address-books/new-addresslist?view=exchange-ps#optional-parameters

Specifically:

If you don't use this parameter,the address list is created under the root (\).

...

Default value: None

$container = $null
New-AddressList -Container $container

# or...

$container = "\"
New-AddressList -Container $container

Comments

-1

take a look at Get-Help *splatting to see a way to do this.

a bit more detail ... a "splat" is a hashtable of parameter = value pairs. once you have the basic always-there items in the splat, you can add others just as you would to any hashtable by $Param_Splat.Add(ParameterName, 'Value'). then when you call your cmdlet, you use Verb-Noun @Param_Splat. note the @ symbol instead of the usual $. [grin]

take care,
lee

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.