Sign in to follow this  
Followers 0
Docfxit

Does a user exist , If not create one

3 posts in this topic

I'd like to know if a user exists.  If it doesn't I'd like to create one.

This script doesn't get to the msgbox on line 24 if the user does exist.

; This script will add a user
;~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^
; Please be caucus:  This WILL add a user to your system
;
;~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^

_NetUser('UserID', 'Password')

; Add User Accounts
Func _NetUser($sName, $sPassword = '', $sGroupName = 'Administrators', $iAutoLogon = 0)
    ; Creates user accounts. Only 1 user can have autologon, if set.
    If Not FileExists(EnvGet('AllUsersProfile') & '\..\' & $sName) Then
        RunWait(@ComSpec & ' /c ' & _
                'Net User ' & $sName & ' ' & $sPassword & ' /add &&' & _
                'Net LocalGroup ' & $sGroupName & ' ' & $sName & ' /add &' & _
                'Net Accounts /MaxPwAge:UnLimited', '', @SW_HIDE)
        If $iAutoLogon Then
            Local $sRegKey = 'HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
            RegWrite($sRegKey, 'DefaultUserName', 'Reg_sz', $sName)
            RegWrite($sRegKey, 'DefaultPassword', 'Reg_sz', $sPassword)
            RegWrite($sRegKey, 'AutoAdminLogon', 'Reg_sz', 1)
        EndIf
    Else
        MSGBOX(0, "", "User " & $sName & "Exists"
EndIf
EndFunc   ;==>_NetUser

Thank you,

Docfxit

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Here is a simple way to check if a local user exists, without having to search for a profile path (which is also not necessarily present) :

If _UserExists("jerome") Then
    MsgBox(0, "", "user exists")
Else
    MsgBox(16, "", "user does not exist")
EndIf

Func _UserExists($sUsername)
    Return IsObj( ObjGet("WinNT://./" & $sUsername & ", user") )
EndFunc

Note you can find on the forum some functions that avoid the use of external commands such as "net user / group ..." : '?do=embed' frameborder='0' data-embedContent>>

Edited by jguinch

Share this post


Link to post
Share on other sites

Thank you very much.

That is a great help.

Docfxit

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0