<\/div>\n<\/aside>\n\n
I would break out the sections for each into separate Switches, but I don’t know the rest of your script logic & how to fit that in.<\/p>\n
I’m not sure how helpful this could be, but I take in the Department variable (in my onboarding script) thus:<\/p>\n
## - Department\nIf ($Department){$Department = \"*$Department*\"}Else{$Department=\"*\"}\nDo {$Department = (Get-ADUser -Filter {(Enabled -ne $False) -and (Department -like $Department)} -Properties Department | Where-Object {$_.Name -NotMatch $Exclusions}).Department |\n Sort -Unique | Out-GridView -PassThru -Title \"$ScriptName`: Choose Appropriate Department...\"}\nUntil ($Department)\n<\/code><\/pre>\n$Department is an optional command-line parameter, and $ScriptName is just the name of the .PS1 script thus:<\/p>\n
$ScriptName = $(([io.fileinfo]$MyInvocation.MyCommand.Definition).BaseName)\n\n<\/code><\/pre>\n…and $Exclusions is a RegEx with the names of the accounts I don’t want to see.<\/p>\n
As to that Out-Gridview above, I hate it. It’s ugly and awkward and probably wouldn’t have been accepted in Windows 3, nevermind the latest hot set-up. So with kind, gracious help from some awesome people here on SpiceWorks, I made up a routine to avoid Out-Gridview:<\/p>\n
##\t\t\t\t\t\tCheck to make sure an existing User Name was entered\nDo{\n\tIf (!($User)){\n\t\tDo{\n\t\t\t$User = (Read-Host \"$ScriptName`: Enter EXISTING User Name to change password\")\n\t\t} Until ($User)\n\t} # End If NULL User parameter\n\tTry{\n\t\t[Array]$User = Get-ADUser -Filter {(Name -Like $User) -or (SAMAccountName -like $User) -or (GivenName -like $User) -or (Surname -like $User)}\n\t}\n\tCatch{}\n\t} # End, if no $User given\n##\t\t\t\t\t\tIf there are multiple matches, let the script user pick\n\tIf ($User.Count -gt 1){\n\t\t$User | Sort Name |\n\t\t FT @{Label=\"`#\";Expression={$User.IndexOf($_)+1}}, @{Label=\"Name\";Expression={$_.Name}}\n## Get the User's choice\n\t\tDo{[Int]$Choice = Read-Host \"$ScriptName`: Choose which User to change password\"} Until ($Choice -gt 0 -And $Choice -le $User.Count)\n## Do something with it\n\t\t$User = $User[$Choice-1]\n\t} # End if there were multiple results. If single, $User already holds it.\n} Until ($User.Name)\n<\/code><\/pre>\nHTH…<\/p>","upvoteCount":0,"datePublished":"2021-04-07T11:41:59.000Z","url":"https://community.spiceworks.com/t/powershell-swith-with-two-parameters/795985/8","author":{"@type":"Person","name":"jimlong3","url":"https://community.spiceworks.com/u/jimlong3"}},{"@type":"Answer","text":"