Reputation: 101
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:
$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.
Upvotes: 8
Views: 13851
Reputation: 32210
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:
#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.
Upvotes: 9
Reputation: 1620
Special treatment is not needed, Windows credentials are case-insensitive so regardless of what case the user enters the domain or username you can pass that to a cmdlet or command line tool and it will just work.
For example, creating a PSCredential with the following usernames will have identical results:
Additionally, by default PowerShell comparison operators are not case sensitive;
"CASE" -eq "case" #returns true
"CASE" -ceq "case" #case-sensitive version returns false
Upvotes: 0