[PS] Checking a valid local user name of an account in Windows 7

rated by 0 users
This post has 6 Replies | 1 Follower

Top 25 Contributor
Posts 37
balubeto Posted: 09-05-2010 5:13 AM

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

Top 25 Contributor
Posts 37

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
}

writes "Username $username is invalid" when I write an username that contains spaces. Why?

THANKS

BYE

Top 25 Contributor
Posts 37

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?

THANKS

BYE

Top 25 Contributor
Posts 37

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

Top 25 Contributor
Posts 37

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"
}

In other words, what is the opposite of the ^\s|\s$|^(\.|\s)+$|.{21,}|[\\/"\[\]:\|<>\+=;,\?\*@] expression to remove at least the -not operator?

THANKS

BYE

Top 25 Contributor
Posts 37

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"
}

By chance, someone sees bugs in this script?

THANKS

BYE

Top 25 Contributor
Posts 37

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"
}

By chance, someone sees bugs in this script?

THANKS

BYE

Page 1 of 1 (7 items) | RSS
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.