Hey all, I am still pretty new at this whole powershell thing but I need to update our AD with department, title, phone numbers, etc (before me users were given a username and email and then left alone). I have a CSV file with the information in it, and I can get it to work for some users but not all. My script currently is

Import-Csv C:\StaffInfo.csv | ForEach-Object {
$department = $_.department
if($department.Length -lt 1)
{$department = $null}
$title = $_.title
if($title.Length -lt 1)
{$title = $null}
$office = $_.office
if($office.Length -lt 1)
{$office = $null}
$StreetAddress = $_.StreetAddress
if($StreetAddress.Length -lt 1)
{$StreetAddress = $null}
$State = $_.State
if($State.Length -lt 1)
{$State = $null}
$postalcode = $_.postalcode
if($postalcode.Length -lt 1)
{$postalcode = $null}
$city = $_.city
if($city.Length -lt 1)
{$State = $null}
$officephone = $_.telephonenumber
if($officephone.Length -lt 1)
{$officephone = $null}
$MobilePhone = $_.Mobile 
if($Mobilephone.Length -lt 1)
{$MobilePhone = $null}

Set-ADUser -Identity $_.name -Department $_.department -title $_.title -Office $_.office -StreetAddress $_.streetAddress -State $_.state -PostalCode $_.postalCode -MobilePhone $_.mobile -OfficePhone $_.telephoneNumber -City $_.city
}

And the error I am getting is:

Set-ADUser : replace
At C:\ADUpdate.ps1:30 char:1
+ Set-ADUser -Identity $_.name -Department $_.department -title $_.titl ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (JohnUser:ADUser) [Set-ADUser], ADInvalidOperationException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser

Some of the fields in the CSV are blank such as:

name,First Name,Last Name,Title,Department,Office,StreetAddress,City,State,PostalCode,telephoneNumber,Mobile,Accreditations
JohnUser,John,User,Systems Administrator,Information Technology,MainOffice,123 Main Street,Anytown,CA,12345,123.555.5555,,
JaneUser,Jane,User,Assistant,,OtherOffice,"555 Second Street, Suite 500",Other Town,WA,54321,,321.888.9900,

Any thoughts?

1 Spice up

Instead of

Import-Csv C:StaffInfo.csv

do this …

Import-Csv C:\StaffInfo.csv

Matt,

In the script it actually is correct, that was a copy/paste error, not a script issue.

I think i figured it out, I was using old code and working with various solutions I had found. I was never referring Set-ADUser to the $null’ed empty values, insted setting everything right and then pointing it back to the imported CSV array. So instead of:

Set-ADUser -Identity $_.name -Department $_.department -title $_.title -Office $_.office -StreetAddress $_.streetAddress -State $_.state -PostalCode $_.postalCode -MobilePhone $_.mobile -OfficePhone $_.telephoneNumber -City $_.city

I used:

Set-ADUser -Identity $_.name -Department $department -title $title -Office $office -StreetAddress $streetAddress -State $state -PostalCode $postalCode -MobilePhone $mobile -OfficePhone $telephoneNumber -City $city

and that seems to work now, it helps when I actually fix all my code to adapt a solution.

2 Spice ups