# Can't get 'switch' statement to work properly

**URL:** <https://forums.powershell.org/t/cant-get-switch-statement-to-work-properly/4452>\
**Category:** PowerShell Help\
**Created:** [July 1, 2015, 2:24am UTC](https://forums.powershell.org/t/cant-get-switch-statement-to-work-properly/4452 "2015-07-01T02:24:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tony-cirino](https://avatars.discourse-cdn.com/v4/letter/t/e79b87/32.png) [@tony-cirino](https://forums.powershell.org/u/tony-cirino)\
**Post date:** [July 1, 2015, 2:24am UTC](https://forums.powershell.org/t/cant-get-switch-statement-to-work-properly/4452/1 "2015-07-01T02:24:18Z")

</div>

I’ve got a messy Exchange 2007 powershell script I’m trying to clean up and convert to Exchange 2013. Among one of the messes is a long set of if/elseif commands I’m trying to move to ‘switch’.

* * *

$databasename=(Get-MailboxDatabase | foreach {Get-ChildItem (“\” + $_.Server + "" + $_.edbFilePath).Replace(“:”,“$”)| Where-Object{$_.Name -ne “Separated.edb” -and $_.Name -ne “Journal.edb”}} | sort -property length)[0].name.TrimEnd(“.edb”)

switch($databasename.startswith){  
“MDB1” {$servername=“[SRV-EXCH1.company.com](http://SRV-EXCH1.company.com)”}  
“MDB2” {$servername=“[SRV-EXCH2.company.com](http://SRV-EXCH2.company.com)”}  
“MDB3” {$servername=“[SRV-EXCH3.company.com](http://SRV-EXCH3.company.com)”}  
“MDB4” {$servername=“[SRV-EXCH4.company.com](http://SRV-EXCH4.company.com)”}  
“MDB5” {$servername=“[SRV-EXCH5.company.com](http://SRV-EXCH5.company.com)”}  
“MDB6” {$servername=“[SRV-EXCH6.company.com](http://SRV-EXCH6.company.com)”}  
“MDB7” {$servername=“[SRV-EXCH7.company.com](http://SRV-EXCH7.company.com)”}

```
default {$servername="Something went wrong"}

```

}

* * *

But for some reason it doesn’t work. I have to be missing something simple. What is it?

[PS}\>$databasename.startswith(“MDB7”)  
True

[PS]\>$databasename  
MDB703  
[PS]\>$servername  
Invalid entry

---

<div class="post-metadata">

**Author:** ![tony-cirino](https://avatars.discourse-cdn.com/v4/letter/t/e79b87/32.png) [@tony-cirino](https://forums.powershell.org/u/tony-cirino)\
**Post date:** [July 1, 2015, 2:26am UTC](https://forums.powershell.org/t/cant-get-switch-statement-to-work-properly/4452/2 "2015-07-01T02:26:00Z")

</div>

Support question.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [July 1, 2015, 2:39am UTC](https://forums.powershell.org/t/cant-get-switch-statement-to-work-properly/4452/3 "2015-07-01T02:39:13Z")

</div>

You can’t use a method like that in your switch statement. However, the switch statement does support -Wildcard and -Regex options, both of which would work here:

```
switch -Wildcard ($databasename) {
    "MDB1*" {$servername="SRV-EXCH1.company.com"}
    "MDB2*" {$servername="SRV-EXCH2.company.com"}
    "MDB3*" {$servername="SRV-EXCH3.company.com"}
    "MDB4*" {$servername="SRV-EXCH4.company.com"}
    "MDB5*" {$servername="SRV-EXCH5.company.com"}
    "MDB6*" {$servername="SRV-EXCH6.company.com"}
    "MDB7*" {$servername="SRV-EXCH7.company.com"}

    default {$servername="Something went wrong"}
}
```

---

<div class="post-metadata">

**Author:** ![tony-cirino](https://avatars.discourse-cdn.com/v4/letter/t/e79b87/32.png) [@tony-cirino](https://forums.powershell.org/u/tony-cirino)\
**Post date:** [July 1, 2015, 2:46am UTC](https://forums.powershell.org/t/cant-get-switch-statement-to-work-properly/4452/4 "2015-07-01T02:46:54Z")

</div>

That did it. Thanks!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:45pm UTC](https://forums.powershell.org/t/cant-get-switch-statement-to-work-properly/4452/5 "2024-05-16T20:45:08Z")

</div>


