PowerShell If Statements: IF equals multiple specific Texts


Good Morning! I’m working on a script and I’m trying to include an IF/Else statement based off a text variable with multiple specific text options and to test for a directory path that will be named after the variable and make the directory if it doesn’t exist. So example would be

Im providing users a list of the valid inputs, that can be entered into the text box. When I try running the script I’m still getting errors saying the folder already exists and it’s not ignoring it like it should be.

A side question is there a cleaner way to write this?


Below is the answer that worked perfect for me. Thank you everyone for your responses!


Looks like you’re trying to create the directories if your user chooses one of 3 text phrases and the directory doesn’t already exist, and complain to your user if they choose something other than the 3 text phrases. I would treat each of those cases separately:

Or you could test for the existence of the directory first like this:

EDIT: Or, if you want to be tricky and avoid the Test-Path (as suggested by @tommymaynard), you can use the following code (and even eliminate the Try|Catch wrappers if you don’t want error checking)

EDIT: Also worth noting that there are many ways to Use PowerShell to Create Folders

Some nice, clean examples. Hope this helps.


PowerShell If Statements: IF equals multiple specific Texts by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply