Hi,

So Ive got my powershell switch working fine but what I was wondering is - can I collect multiple parameters from one switch result? So - below I have my switch

$Company = Read-Host " Please select the company"
switch ($Company)
{
‘1’ { $Company = “Company1”
}
‘2’ { $Company = “Company2”
}
}

But if for example someone selects 1 I want to be able to collect additional parameters like $Address $ Postcode

Is this possible?

Thanks

2 Spice ups

Add this in the case

$Address = Read-Host -Prompt ‘Enter your address’

$Postal = Read-Host -Prompt ‘Enter your post’

Thanks - if thats the case, is there a way to use an if statement so that IF user selects 1 then the switch parameter for the company is used and the necessary post codes etc are used?

Please describe what it it you are actually trying to do.

Using read-host to get parameters is not really a good idea, you’re better off using -parameters and the established commandlet syntax. If this is a user facing script I recommend you have a look at How to add a Graphical User Interface to your PowerShell Functions using the .Net SystemWindowsForm class .

Having said that, try the following:

# Option 1
$Company = Read-Host -Prompt 'Please select the company'
$Address = Read-Host -Prompt 'Enter your address'
$Postal = Read-Host -Prompt 'Enter your post'
# Overwrite $Company with the company name
switch ($Company) {
    '1' {$Company = "Company1"}
    '2' {$Company = "Company2"}
    default {$Company = "ACME Inc."} # Just kidding ;)
}

# Option 2
# Define company defaults
$CompanyDefaults['Company1'] = @{Name = 'Company 1'; Address = "Somewhere"; Postal = "99358"}
$CompanyDefaults['Company2'] = @{Name = 'Company 2'; Address = "Somewhere else"; Postal = "99359"}
$CompanyDefaults['ACME Inc.'] = @{Name = 'ACME Inc.'; Address = "42"; Postal = "42"}

$Company = Read-Host -Prompt 'Please select the company'
$Address = Read-Host -Prompt 'Enter your address'
$Postal = Read-Host -Prompt 'Enter your post'
# Overwrite $Company with the company name
switch ($Company) {
    '1' {$Company = $CompanyDefaults['Company1'].Name}
    '2' {$Company = $CompanyDefaults['Company2'].Name}
    default {$Company = $CompanyDefaults['ACME Inc.'].Name}
}
# Check $Address and $Postal are not empty
if ($Address.Length -eq 0) {$Address = $CompanyDefaults|Where-Object Name -eq $Company|Select-Object -ExpandProperty Address}
if ($Postal.Length -eq 0) {$Postal = $CompanyDefaults|Where-Object Name -eq $Company|Select-Object -ExpandProperty Postal}

Thanks I used a IF statement rather than a switch statement to get the details into the user account which works fine simular to your option 2 example above. Thanks

If I wanted to add a user to multiple groups - is the only way to reference a text\csv file with those group names in? Or is there a way to achieve in this with powershell?

Thanks

This is a different question and should be asked in a different thread.

Well powershell has to know the names, so importing it from some source is required.

Options are manual input, reading from a file (csv,txt,json,xml etc), or having another cmdlet produce the list (e.g. get-aduser $username -properies memberof) so all the goups a user is memberof will be added.