setting user attributes from a CSV in Powershell

I want to write account attributes such as title , department and telephone numbers, etc... to my users.  I was able to build the script but I found it failed on users who did not have something in every field.  Therefore I created an IF series to build the "Set-User" command.  The command format looks good but it will not run if I simply build the entire statement in a varriable
$setuser = ("Set-User -Identity " + $Alias)
IF ($Office -ne $blank) {$SetUse = ($SetUser + " -Office " + $Office)}
IF ($StreetAddress -ne $blank) {$SetUsr = ($SetUser + " -StreetAddress " + $StreetAddress)}
IF ($City -ne $blank) {$SetUsr = ($SetUser + " -City " + $City)}
IF ($State -ne $blank) {$SetStr = ($SetUser + " -StateOrProvince " + $State)}
IF ($Zip -ne $blank) {$SetUsr = ($SetUser + " -PostalCode " + $Zip)}
IF ($Department -ne $blank) {$SetUser = ($SetUser + " -Department " + $Department)}
IF ($Title -ne $blank) {$SetUser = ($SetUser + " -Title " + $Title)}
IF ($OfficePhone -ne $blank) {$SetUser = ($SetUser + " -Telephone " + $OfficePhone)}
IF ($OfficePhone2 -ne $blank) {$SetUser = ($SetUser + " -OtherTelephone " + $OfficePhone2)}
IF ($MobilePhone -ne $blank) {$SetUser = ($SetUser + " -MobilePhone " + $MobilePhone)}
IF ($Fax -ne $blank) {$SetUser = ($SetUser + " -Fax " + $Fax)}
IF ($Fax2 -ne $blank) {$SetUser = ($SetUser + " -OtherFax " + $Fax2)}
IF ($Pager -ne $blank) {$SetUser = ($SetUser + " -Pager " + $Pager)}
IF ($HomePhone -ne $blank) {$SetUser = ($SetUser + " -HomePhone " + $HomePhone)}
IF ($HomePhone2 -ne $blank) {$SetUser = ($SetUser + " -OtherHomePhone " + $HomePhone2)}
IF ($AssistantName -ne $blank) {$SetUser = ($SetUser + " -AssistantName " + $AssistantName)}
IF ($TelephoneAssistant -ne $blank) {$SetUser = ($SetUser + " -TelephoneAssistant " + $TelephoneAssistant)}
IF ($Webpage -ne $blank) {$SetUser = ($SetUser + " -WebPage " + $Webpage)}
All this does is print on the screen the command but it does not run the command.
So I tried settings the varriable after the "set-user" command and I get
$SetUser = ("-Identity " + $Alias)
(all the IF Statements)
Set-User $SetUser}

The problem here is that it does not understand the individual data in the varriable.

Any help????
Who is Participating?
To answer your question specifically though

change the

Set-User $SetUser
invoke-expression "Set-User $SetUser"

Open in new window

I would recommend the Quest cmdlets. The Exchange cmdlets (from my experience) are very limited in functionality.
Denver_ITAuthor Commented:
Thanks for the solution.  using the invoke-expression command requires me to quote any attributes that have spaces in them.  I did not have to do that if I ran the command manually.  This means I have to ensure the CSV has trippled quotes (when reading it from notepad) so that the varriables show up corrctly

Example: $Address will equal "Long Drive" and in the CSV it must be listed as """Long Drive""",

Thanks again
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.