HI The script: if( $UserName -notmatch "\W") { Write-Host "Username '$UserName' is valid" } Else { Write-Host "Username '$UserName' is invalid" } Code (markup): 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" } Code (markup): 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 } Code (markup): writes "Username $username is invalid" when I write an username that contains spaces. Why? THANKS BYE
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? THANKS BYE