I'm trying to import en employee list into AD using powershell. The script needs to update exisisting AD objects.

The problem I have is that some users will have mobile number some internal numbers and some just an extension, when i run the Set-ADuser command in its most basic form it seems to fail whenever a value is blank.

I'm not great at scripting, can anyone explain to me why the script below doesnt work? And if so could anyone suggest how to make it work? Cheers

Import-module ActiveDirectory

$CSVPath = "c:\scripts\ictdepartment2.csv" 
$csvData = Import-CSV $CSVPath -delimiter "," -Header ("SamaccountName","identity","Title","Company","Department","Description","Office","OfficePhone","IPPhone","MobilePhone") 


foreach ($line in $csvData) 
$accountTable = @{ 
'SamaccountName'= $line.SamaccountName 
'identity'= $line.identity
'title'= $line.title 
'company'= $line.company 
'department'= $line.department 
'description'= $line.description 
'office'= $line.office
'officephone'= $line.officephone 
'IPPhone'= $line.ipPhone 
'mobilephone'= $line.mobilephone 


ForEach($line in $csvData) 
$SamaccountName = $line.SamaccountName
$identity = $line.identity
$title = $line.title 
$company = $line.company 
$department = $line.department 
$description = $line.description 
$office = $line.office
$officephone = $line.officephone
$IPPhone = $line.ipPhone 
$mobilephone = $line.mobilephone 


ForEach($line in $csvData) 
if ($officephone -ne $null)
{Set-ADUser -Identity $SamaccountName -OfficePhone $officephone}
if ($mobilephone -ne $null)
{Set-ADUser -Identity $SamaccountName -MobilePhone $mobilephone}
if ($ipphone -ne $null)
{Set-ADUser -Identity $SamaccountName -replace @{ipPhone=$ipphone}}

Set-ADUser -Identity $SamaccountName -Company $Company -Department $Department -Description $Description -Office $office -Title $title

If i use the following commands it works, however as soon as i add the if $null statement in it again fails :(

ForEach($line in $csvData) 
Get-ADUser -Filter "SamAccountName -eq '$($line.samaccountname)'" |
Set-ADUser -Replace @{ipphone=$ipphone}

ForEach($line in $csvData) 
Get-ADUser -Filter "SamAccountName -eq '$($line.samaccountname)'" |
Set-ADUser -Company $Company -Department $Department -Description $Description -MobilePhone $mobilephone -Office $office -OfficePhone $officephone -Title $title

$null and an empty string ("") are two different data types to powershell. Replace any instance of $null with "" and you should be good. Almost all MS provided cmdlets are set to [ValidateNotNull].

I'd like to expand a bit on what $Null is. Powershell is an OOP (Object Oriented Programming) shell with direct hooks into .Net. $Null is a placeholder and an object in it's own right that represents nothing. Don't worry about fully understanding $Null at this point, a lot of people (even some programmers) don't understand $Null to the Nth degree.

So why is this relevant now? Let me link you to THIS REFERENCE but I'll copy the relevant info here...

...you cannot use $null to mean "no parameter value." A parameter value of $null overrides the default parameter value.

In your case you're trying to equate one variable's contents to the $Null object, it'll never match because only $Null is -eq to $Null.

An empty string ("") is a String object without a value. Which is why you want to use it with comparison operators (-eq, -lt, -ge) to evaluate other strings.

  • Great reply thanks, a lot easier when you know what your trying to do is fundamentally wrong – Dan Nov 16 '15 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.