+ Post New Thread
Results 1 to 2 of 2
Scripts Thread, Powershell - Check if user exists in Coding and Web Development; Evening I've been updating my new user script and setting it up in powershell, the intention being it will later ...
  1. #1
    Cache's Avatar
    Join Date
    Apr 2008
    Location
    Cumbria
    Posts
    1,116
    Thank Post
    414
    Thanked 149 Times in 147 Posts
    Blog Entries
    3
    Rep Power
    59

    Powershell - Check if user exists

    Evening

    I've been updating my new user script and setting it up in powershell, the intention being it will later be combined into calling some powershell for Office 365.

    I've got it all working apart from one small part, it fails to check if a user already exists.

    Could someone look to see where I've gone wrong please?

    Code:
    $usercheck = $null
    # check if username exists already in AD
    $usercheck = Get-ADUser -LDAPFilter "(sAMAccountName=$_.SamAccountName)"
    
    # Check search result to see if it's Null and then create the user
    If ($usercheck -eq $null) {
    
    Write-Host "User Check Completed"
    }
    # If it's not null, print out to screen
    Else { Write-Host "User: "$_.SamAccountName "already exists!"}
    }
    At the moment, regardless of whether the user exists or not, the logic seems to assume it doesn't and runs through the creation script.

    It's a shame because it took me all day yesterday and while I understand what most of it is doing, I don't understand this simple logic.

    Thanks!

  2. #2
    Cache's Avatar
    Join Date
    Apr 2008
    Location
    Cumbria
    Posts
    1,116
    Thank Post
    414
    Thanked 149 Times in 147 Posts
    Blog Entries
    3
    Rep Power
    59
    Never mind, fixed it myself when I've taken a more objective look and a bit more searching.

    Modified to the following:

    Code:
    $name = $_.SamAccountName
    
    
    $usercheck = $null
    # check if username exists already in AD
    $usercheck = Get-ADUser -Filter {sAMAccountName -eq $name}
    
    # Check search result to see if it's Null and then create the user
    If ($usercheck -eq $null) {
    
    Write-Host "User Check Completed"
    }
    # If it's not null, print out to screen
    Else { Write-Host "User: "$_.SamAccountName "already exists!"}
    }
    Still don't understand why the first conditional failed though :/
    Last edited by Cache; 28th June 2013 at 06:10 PM.

SHARE:
+ Post New Thread

Similar Threads

  1. Check if user has read document
    By karldenton in forum Windows
    Replies: 3
    Last Post: 27th November 2012, 05:25 PM
  2. quick check if accounts exist in AD
    By RabbieBurns in forum Windows Server 2008 R2
    Replies: 1
    Last Post: 3rd February 2012, 01:19 AM
  3. Replies: 1
    Last Post: 5th April 2011, 05:17 PM
  4. Replies: 17
    Last Post: 19th March 2010, 11:51 PM
  5. Replies: 3
    Last Post: 1st March 2006, 12:08 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •