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

Discussion in 'Programming' started by balubeto, Sep 5, 2010.

  1. #1
    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
     
    balubeto, Sep 5, 2010 IP
  2. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #2
    hmm, is this a batch script?
     
    Rainulf, Sep 5, 2010 IP
  3. balubeto

    balubeto Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    balubeto, Sep 8, 2010 IP
  4. balubeto

    balubeto Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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
     
    balubeto, Sep 10, 2010 IP