Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell case sensitivity variables

Tags:

powershell

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.

like image 370
Tula Avatar asked Jul 03 '16 15:07

Tula


1 Answers

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.

like image 90
Bacon Bits Avatar answered Oct 03 '22 03:10

Bacon Bits