2

I got the following code (working):

   #Import File
$Users = Import-Csv C:\Users\Administrator\Desktop\userImport\users.csv

# Setting data
$compname = hostname
$computer = [ADSI]"WinNT://$compname" 
$userGroup = [ADSI]"WinNT://./Users,Group"

# Loop to add all users
$Users | %  { 

    # Create user itself
    $createUser = $computer.Create("User",$_.userid)

    # Set password (print1!)
    $createUser.SetPassword($_.password)
    $createUser.SetInfo()

    # Create extra data
    $createUser.Description = "Import via powershell"
    $createUser.FullName = $_.'full name'
    $createUser.SetInfo()

    # Set standard flags (Password expire / Password change / Account disabled)
    $createUser.UserFlags = 64 + 65536 # ADS_UF_PASSWD_CANT_CHANGE + ADS_UF_DONT_EXPIRE_PASSWD
    $createUser.SetInfo()

    # Adduser to standard user group ("SERVER02\Users")
    $userGroup.Add($createUser.Path)

    # Show that user X as been added
    $username = $_.'full name'
    echo "User  $username added"
}

But how is possible to build an check if the user already exists? I can not really find anything on the web :x

Joe Wicentowski
  • 5,159
  • 16
  • 26
Patrick Rennings
  • 191
  • 1
  • 5
  • 19

3 Answers3

2

You can try this:

   #Import File
$Users = Import-Csv C:\Users\Administrator\Desktop\userImport\users.csv

# Setting data
$compname = hostname
$computer = [ADSI]"WinNT://$compname" 
$userGroup = [ADSI]"WinNT://./Users,Group"

$localUsers = $computer.Children | where {$_.SchemaClassName -eq 'user'}  |  % {$_.name[0].ToString()}

# Loop to add all users
$Users | %  { 

    if($localUsers -NotContains $_.userID)
    {
      ... do your job here....
    }
    else
    {
      write-host "User $($_.userID) already exists"
    }
}
CB.
  • 58,865
  • 9
  • 159
  • 159
  • "user @{Full Name=P. Reichardt; userid=paj.Reichardt; Password=xxx}.userid does not exist." is what I get back with your script when I check with $_.userid #Import File $Users = Import-Csv C:\Users\Administrator\Desktop\userImport\userTest.csv # Loop to add all users $Users | % { $localUsers = $computer.Children | where {$_.SchemaClassName -eq 'user'} | % {$_.name[0].ToString()} if ($localUsers -NotContains $_.userid) { write-host "user $_.userid does not exist." } else { write-host "user $_.userid already exists." } } – Patrick Rennings Dec 03 '12 at 15:06
  • @PatrickRennings Edited my answer with variable expansion correctly set! – CB. Dec 03 '12 at 15:18
  • $_.userid is now filled with a large line: user @{Full Name=P. Reichardt; userid=paj.Reichardt; Password=xxx}.userid does not exist" – Patrick Rennings Dec 03 '12 at 15:21
  • @PatrickRennings I've test it with a .csv with 3 columns and one named column as userID that has the account name and it works. Try cut&paste my code and run it. – CB. Dec 03 '12 at 15:31
0

unfortunately, the ADSI interface seems to be broken in Windows 8.1. here's my workaround:

if (& net users | select-string "UserName") {
    #  user exists
} else {
    #  user not found
}
burton
  • 421
  • 4
  • 13
0

You can use the static Exists function of ADSI to check if an user exists:

[ADSI]::Exists('WinNT://./username')

But take note of this: [ADSI]::Exists throws an exception instead of returning False

Community
  • 1
  • 1
mhu
  • 17,720
  • 10
  • 62
  • 93