How to Check for Null Values in CSV File for Creating New-ADUsers Script?


  • I'm a bit new to this. I understand how the if statement works but can't understand how to best do this with all the parameters I have to check for. 

    I'm already using an IF Else statement to check an ISACTIVE column because I need to have certain user accounts enabled/disabled. I tried passing values to the -Enabled AD parameter but ended up having to do this because it wouldn't take it.

    I'm confused on where to add the extra if statements to do this check. Here is my code so far. I've seen examples from others using $hash=@{} but don't know how I can implement it into my current script. SIGH!

    $csv = Import-csv 'C:\PSScripts\(ProdTest).csv' -Delimiter '|'
    Foreach ($user in $csv) {
    $name = $givenName + " " + $surName
    $sAMAccountName = $user.USER_NAME
    $officePhone = $user.PHONENUMBER
    $emailAddress = $user.EMAIL
    $givenName = $user.FNAME
    $surName = $user.LNAME
    $company = $user.COMPANY
    $department = $user.DEPARTMENT
    $streetAddress = $user.ADDRESS
    $city = $user.CITY
    $state = $user.STATE
    $country = $user.COUNTRY
    $postalCode = $user.ZIPCODE
    $accountPassword = $user.PASSWORD
    $enabled = $user.ISACTIVE
    $OU = "ou=Application,dc=contoso,dc=lab"
    $upn = $sAMAccountName + "@contoso.lab"
    if ($user.ISACTIVE -eq 'Y') {
    new-aduser -Name $name `
               -SamAccountName $sAMAccountName `
               -OfficePhone $officePhone `
               -EmailAddress $emailAddress `
               -Path $OU `
               -DisplayName $name `
               -UserPrincipalName $upn `
               -GivenName $givenName `
               -Surname $surName `
               -Department $department `
               -City $city `
               -State $state `
               -PostalCode $postalCode `
               -AccountPassword (ConvertTo-SecureString $accountPassword -AsPlainText -Force) `
               -Enabled $true `
               -ChangePasswordAtLogon $false }
       else {
        new-aduser -Name $name `
                   -SamAccountName $sAMAccountName `
                   -officePhone $officePhone `
                   -EmailAddress $emailAddress `
                   -Path $OU `
                   -DisplayName $name `
                   -UserPrincipalName $upn `
                   -GivenName $givenName `
                   -Surname $surName `
                   -Department $department `
                   -City $city `
                   -State $state `
                   -PostalCode $postalCode `
                   -AccountPassword (ConvertTo-SecureString $accountPassword -AsPlainText -Force) `
                   -Enabled $false `
                   -ChangePasswordAtLogon $false }}

    Thursday, November 21, 2013 4:43 PM


All replies

  • Hi,

    Sorry for the delay, I'm not really sure why Yan wanted you to start a new thread.

    Here's an example of how you can use If statements to check for null variables and set them to be a single blank character:

    If (!($officePhone)) { $officePhone =' ' }
    If (!($company)) { $company =' ' }
    If (!($streetAddress)) { $streetAddress =' ' }

    Use as many of them as you need, and put them above your existing If/Else statement.

    Don't retire TechNet! - (Maybe there's still a chance for hope, over 12,420+ strong and growing)

    • Marked as answer by IWAR Thursday, November 21, 2013 11:47 PM
    Thursday, November 21, 2013 4:51 PM
  • What exactly isn't working? Do you have an ISE that you can step through the code to see where it isn't working?

    Also, I believe you will need to move $name = $givenName + " " + $surName down the list, as you define $givenName and $surName, after $name, I could be wrong though, other than that, I do not see anything jumping at me that would cause a failure.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Don't Retire Technet

    Thursday, November 21, 2013 4:52 PM
  • Hmm.. I'll try this one out and reply back with my results. 
    Thursday, November 21, 2013 7:58 PM
  • Good call out! I'll move it down... I just started using ISE sooo I'm still learning how to use it... Add to that I don't have much/close to no experience with debugging... Trying to learn as much as I can though...

    Thanks for the help!

    Thursday, November 21, 2013 8:50 PM
  • The ISE is definitely a great tool. Do yourself a favor and make sure you're running at least PS 3.0. The ISE is much improved from 2.0.

    Don't retire TechNet! - (Maybe there's still a chance for hope, over 12,420+ strong and growing)

    Thursday, November 21, 2013 8:56 PM
  • Good call! 

    So going back to your suggestion on changing those values to blank spaces - I don't want to set all that empty data in AD. 

    How do I write it to only list a parameter and value only if there is a value for it in the CSV file? 

    I saw this example someone used and have been trying to figure it out. Don't these properties need a "-" before them if you list them after New-ADUser? Also, don't you need a value after @list?

    $Properties = "GivenName","Initials","Surname","Name","DisplayName","Description","Office","OfficePhone","EmailAddress","HomePage","StreetAddress","POBox","City","State","PostalCode","Country"
    	$list = @{}
    	 foreach ($property in $properties){
    		  $list.$property = $_.$property}
    	New-ADUser @list

    Thursday, November 21, 2013 9:53 PM
  • That code would require that your CSV file's headers exactly match the corresponding parameters to the New-ADUser cmdlet.  Yours don't, so you'd have to have some additional code to translate between the two.

    To help with understanding what it's doing, though, it builds a hash table, and then "splats" the hash table as named parameters to New-ADUser; that's what the @list notation does.  You can read more about this in the "about_Splatting" help file.

    Thursday, November 21, 2013 10:47 PM
  • David and IWAR are on a good track, but you can use a hash table and filter remove the Null values to get what you want. Just do this for the name assignments:

    $csv = Import-csv 'C:\PSScripts\(ProdTest).csv' -Delimiter '|'
    Foreach ($user in $csv) {
    	$UserProps.sAMAccountName = $user.USER_NAME
    	$UserProps.officePhone = $user.PHONENUMBER
    	$UserProps.emailAddress = $user.EMAIL
    	$UserProps.givenName = $user.FNAME
    	$UserProps.surName = $user.LNAME
    	$UserProps.Name = $UserProps.givenName + " " + $UserProps.surName
    	$ = $user.COMPANY
    	$UserProps.department = $user.DEPARTMENT
    	$UserProps.streetAddress = $user.ADDRESS
    	$ = $user.CITY
    	$UserProps.state = $user.STATE
    	$ = $user.COUNTRY
    	$UserProps.postalCode = $user.ZIPCODE
    	$UserProps.accountPassword = $user.PASSWORD
    	$UserProps.enabled = $user.ISACTIVE
    	$UserProps.OU = "ou=Application,dc=contoso,dc=lab"
    	$UserProps.upn = $UserProps.sAMAccountName + "@contoso.lab"
    	$UserProps.ChangePasswordAtLogon = $false
    	ForEach ($key in $UserProps.keys) {
    		If (($UserProps.$key -ne $null) -and ($UserProps.$key -ne "")) {
    	New-ADUser @ADProps

    • Edited by SmallFoxx Thursday, November 21, 2013 11:21 PM putting in code as code snippet
    • Proposed as answer by SMFX Tuesday, November 26, 2013 10:21 PM
    Thursday, November 21, 2013 11:06 PM
  • Thank you so much SmallFoxx! I'm going to test this out as well. 

    I used Mike's if statement suggestion and it worked. Since I was worried about setting them to blank spaces, I checked in AD for those user accounts and those values were not set with a blank space. THANK YOU MIKE! 

    Thursday, November 21, 2013 11:47 PM
  • Cheers. You're very welcome, I'm glad I was able to help out.

    Don't retire TechNet! - (Maybe there's still a chance for hope, over 12,420+ strong and growing)

    Friday, November 22, 2013 1:23 AM