1

I am trying to figure out how to test for mixed case or change the user input to mixed case.

Currently my code consists of:

$Type = Read-Host 'Enter MY, OLD, NEWTest, Old_Tests'

However, I need to validate that the user entered in the exact case, and if they didn't change the case to the correct case. I have reviewed so many different questions on here and other websites, but none seem to really talk about validating mixed case in a way I can understand.

  1. Validate User Entry
  2. Regex to match mixed case words
  3. Validating String User Entry
  4. How to check if a string is all upper case (lower case) characters?
  5. Learn Powershell | Achieve More
  6. How To Validate Parameters in PowerShell
  7. $args in powershell

I am not asking anyone to write code for me. I am asking for some sample code that I can gain an understanding on how to validate and change the entries.

Community
  • 1
  • 1
RadFox
  • 419
  • 1
  • 4
  • 17

1 Answers1

3

PowerShell performs case-insensitive comparisons by default, so to answer the first part of your question you need to do a case-sensitive comparison which is -ceq.

$Type = Read-Host 'Enter MY, OLD, NEWTest, Old_Tests'
($Type -ceq 'MY' -or  $Type -ceq 'OLD' -or  $Type -ceq 'NEWTest' -or  $Type -ceq 'Old_Tests')

Although a simpler solution to that is to use case-sensitive contains -ccontains:

('MY', 'OLD', 'NEWTest', 'Old_Tests' -ccontains $Type)

Here's one way you might correct the case:

$Type = Read-Host 'Enter MY, OLD, NEWTest, Old_Tests'

If ('MY', 'OLD', 'NEWTest', 'Old_Tests'  -cnotcontains $Type){

    If ('MY', 'OLD', 'NEWTest', 'Old_Tests' -contains $Type){
        $TextInfo = (Get-Culture).TextInfo

        $Type = Switch ($Type) {
            {$_ -in 'MY','OLD'} { $Type.ToUpper() }
            'NEWTest'           { $Type.Substring(0,4).ToUpper() + $Type.Substring(4,3).ToLower() }
            'Old_Tests'         { $TextInfo.ToTitleCase($Type) }
        }  
    } Else {
        Write-Warning 'You didnt enter one of: MY, OLD, NEWTest, Old_Tests'
    }
}

Write-Output $Type

Explanation:

First we test if the case is correct for the four permitted words (-cnotcontains Case Sensitive Not Contains), if it is we do nothing. If the case is not correct, then we test the text is correct (without caring about case sensitivity -contains).

If the text is correct then we use Switch statement to deal with the different scenarios that we want to adapt the case for:

The first switch test matches the first two words and simply uppercases them with the ToUpper() string method.

The second switch test uses the string method SubString to get a subset of the string starting from the first character (0) and taking 4 characters in length. We uppercase this with ToUpper then we add on the next 3 characters of the string, starting at the 4th character, which we force in to lower case with ToLower().

The final switch test we handle with a .NET method taken from the get-culture cmdlet which allows us to Title Case a string (make the first letter of each word uppercase).

If the inputted text didn't match one of the options we use write-warning (may require PowerShell 4 or above, if you don't have this change it to write-host) to print a warning to the console.

Finally whatever was entered we send to stdout with Write-Output.

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
  • how can I change newtest to NEWTest? – RadFox Mar 29 '17 at 13:35
  • Sorry Mark, I was tied up with other items yesterday and didn't get a chance to validate your tutelage. I have tested your solution out and it is still not changing the case to upper or lower. Enter MY, OLD, NEWTest, Old_Tests: newtest Write-Output = newtest – RadFox Mar 30 '17 at 13:00
  • My solution assumed you wanted to handle the whole string at once, not deal with the individual options. I'll amend my answer to deal with that. – Mark Wragg Mar 30 '17 at 13:19
  • 1
    Awesome blog by the way... I'll be reading multiple of your articles. You explain things in English like I understand. – RadFox Mar 30 '17 at 13:28
  • Thanks, glad you like it! I've revised my answer to deal with the input being any one of the 4 individually entered strings. – Mark Wragg Mar 30 '17 at 13:34
  • I've got everything working and fixed the issue where I'm trying to convert "old_tests" to "OLD_Tests". I now understand how to handle the uppercase and lowercase of items. I now have working code for more than what you helped me understand. – RadFox Mar 30 '17 at 15:15