Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell update value with concatenated objects

Posted on 2015-02-24
6
Medium Priority
?
141 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 43

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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 41

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Let's recap what we learned from yesterday's Skyport Systems webinar.
Microsoft has changed the look and feel of Azure AD and Microsoft account sign-in pages so that you will have a more unified look and feel when moving between the two interfaces.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
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.

604 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