?
Solved

Powershell update value with concatenated objects

Posted on 2015-02-24
6
Medium Priority
?
139 Views
Last Modified: 2015-03-04
I am trying to update the UPN for a list of users in O365.  I have connected via Powershell and am able to update individual entries however my Powershell is not strong enough to do them all in one shot.

Get-MsolUser | where-object {$_.UserPrincipalName -eq ($_.firstname) +
"." + ($_.lastname) + "@domain.com"} | Set-MsolUserPrincipalName -NewUserPrincipalName ($_.firstname.substring(0,1)) + ($
_.lastname) + "@domain.com"

you cannot call a method on a null-valued expression.
At line:1 char:1
+ Get-MsolUser | where-object {$_.UserPri ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

basically trying to set the UPN to first initial + lastname instead of the firstname.lastname.  I have fixed dirsync to create the UPN this way however need to fix all the users which are set the wrong way.  Any help with the code would be appreciated.
0
Comment
Question by:YMartin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40629004
Something like this should work but not tested.

Get-MsolUser | where-object {$_.UserPrincipalName -eq ($_.firstname) | %  {
$nuname = ($_.firstname.substring(0,1)) + ($_.lastname) + "@domain.com"
Set-MsolUserPrincipalName -NewUserPrincipalName $nuname 
}

Open in new window

0
 
LVL 1

Author Comment

by:YMartin
ID: 40629060
The problem is that the set-msoluserprincipalname also requires the old userprincipalname.  Both would need to be stored in the variable and be called individually.  Tried to accomplish this but it did not work for me.  It would have piped it for my command but with your version it would need to be saved to the variable.
0
 
LVL 42

Expert Comment

by:Vasil Michev (MVP)
ID: 40629206
You already have the UserPrincipalName going through the pipeline, $_.UserPrincipalName. The brackets are messed up though.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Accepted Solution

by:
footech earned 2000 total points
ID: 40629581
I prefer subexpressions to concatentation so I've done that below.  The only issue with becraig's was that it needed to specify the -UserPrincipalName parameter for Set-MsolUserPrincipalName.  You could use the intermediate variable, but I've just specified the value directly.
Get-MsolUser | where-object {$_.UserPrincipalName -eq "$($_.firstname).$($_.lastname)@domain.com"} | % {
    Set-MsolUserPrincipalName -UserPrincipalName $_.UserPrincipalName -NewUserPrincipalName "$($_.firstname.substring(0,1))$($_.lastname)@domain.com"
}

Open in new window

0
 
LVL 29

Expert Comment

by:becraig
ID: 40629602
yup I did forget to add the piece footech thanks .
Get-MsolUser | where-object {$_.UserPrincipalName -eq ($_.firstname) | %  {
$nuname = ($_.firstname.substring(0,1)) + ($_.lastname) + "@domain.com"
Set-MsolUserPrincipalName  -Userprinicpalname $_.UserPrincipalName -NewUserPrincipalName  $nuname 
}
                                          

Open in new window

0
 
LVL 1

Author Closing Comment

by:YMartin
ID: 40645729
Thanks footech.  That worked.  Sorry I was not able to get the code from becraig to work but I appreciate the help.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question