Question:
I want to write a script that does a manipulation on users in my company.
Usernames can be with capital letters/small letters, and also the domain name is sometimes assigned to them with capital letters, so a username can be like:
domain\username, DOMAIN\USERNAME, DOMAIN\username or domain\USERNAME.
I ask for the username like this:
1 2 |
$user = Read-Host "Please insert username" |
How can I make $user
non case sensitive and also the company name?
The username needs to be like $company\$user
without case sensitivity.
Answer:
String comparisons in PowerShell are typically case-insensitive by default.
Strings themselves are case aware, meaning they know that an A
is a different glyph than an a
and it will remember which was used, but the normal comparison operators (-eq
, -match
, -like
, -lt
, -in
, etc.) are all case-insensitive.
You have to specify the case-sensitive versions for case-sensitive comparisons (-ceq
, -cmatch
, -clike
, -clt
, -cin
, etc.). You can also specify the explicitly case-insensitive operators (-ieq
, -imatch
, -ilike
, -ilt
, -iin
, etc.).
If you want to force the characters to a specific case, you can do this:
1 2 3 4 5 6 |
#Set characters to lower case $user = $user.ToLower(); #Set characters to upper case $user = $user.ToUpper(); |
But there is no property of strings that marks them as inherently case-insensitive.