HI
The script:
if( $UserName -notmatch "\W") { Write-Host "Username '$UserName' is valid"}Else { Write-Host "Username '$UserName' is invalid"}
works, but, if a user does not insert any characters, the script writes: "Username '' is valid" Why?
Instead, this script:
If ( (([adsi]("WinNT://[Environment]::MachineName,computer")).children | ? {$_.psbase.schemaClassName -eq "User"} | Select -expand Name) -contains $UserName) { Write-host "$UserName exists"}else { Write-host "$UserName not exists"}
does not work because it always writes that the user inserted does not exist. Why?
THANKS
BYE
I noticed that the script:
$UserName = Read-Host "Checking the validity of the username" if ( $UserName -match "^\w{1,19}$" ) #Please note the lowercase w, means: 1 to 19 characters of the character class [a-zA-Z0-9_]{ Write-Host -Foreground green "Username '$UserName' is valid" #Now we take the valid case in the IF clause because it is easier to write }else{ Write-Host -Foreground red "Username '$UserName' is invalid" # and the invalid case in the ELSE clause}
$UserName = Read-Host "Checking the validity of the username"
if ( $UserName -match "^\w{1,19}$" ) #Please note the lowercase w, means: 1 to 19 characters of the character class [a-zA-Z0-9_]{ Write-Host -Foreground green "Username '$UserName' is valid" #Now we take the valid case in the IF clause because it is easier to write }else{ Write-Host -Foreground red "Username '$UserName' is invalid" # and the invalid case in the ELSE clause}
writes "Username $username is invalid" when I write an username that contains spaces. Why?
In Windows 7, the symbols that can not be used in the usernames are only /\[]":;<>+=,?*
If this is true, how can I check if a name of a user contains all the symbols allowed by Windows 7 (multilingual) except the symbols listed above?
The "^[\w+\p{L}\s-]{1,17}$" expression works well, but unfortunately, it also validates the usernames that begin or end with spaces. So, how could I fix this?
In Windows 7, what is the maximum length for the usernames?
It is possible to improve the look of the condition of the IF structure of this script?
$UserName = Read-Host "Checking the validity of the username" if (($username.length -ge 1) -and -not(([regex]::isMatch($username,'^\s|\s$|^(\.|\s)+$|.{21,}|[\\/"\[\]:\|<>\+=;,\?\*@]')))) { Write-Host "Username '$UserName' is valid"}else { Write-Host "Username '$UserName' is invalid"}
if (($username.length -ge 1) -and -not(([regex]::isMatch($username,'^\s|\s$|^(\.|\s)+$|.{21,}|[\\/"\[\]:\|<>\+=;,\?\*@]')))) { Write-Host "Username '$UserName' is valid"}else { Write-Host "Username '$UserName' is invalid"}
In other words, what is the opposite of the ^\s|\s$|^(\.|\s)+$|.{21,}|[\\/"\[\]:\|<>\+=;,\?\*@] expression to remove at least the -not operator?
I think I found a more elegant solution or there is another even better solution than this:
$UserName = Read-Host "Checking the validity of the username" if ( ($username.length -ge 1) -and ($UserName -notmatch '^\s|\s$|^(\.|\s)+$|.{21,}|[\\/"\[\]:\|<>\+=;,\?\*@]') ) { Write-Host "Username '$UserName' is valid"}else { Write-Host "Username '$UserName' is invalid"}
if ( ($username.length -ge 1) -and ($UserName -notmatch '^\s|\s$|^(\.|\s)+$|.{21,}|[\\/"\[\]:\|<>\+=;,\?\*@]') ) { Write-Host "Username '$UserName' is valid"}else { Write-Host "Username '$UserName' is invalid"}
By chance, someone sees bugs in this script?