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
Solved

Powershell update value with concatenated objects

Posted on 2015-02-24
6
131 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
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 40

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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 40

Accepted Solution

by:
footech earned 500 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Adoption of Microsoft’s Enterprise Mobility and Security solution and Office 365 will re-order the File Sync and Share market Microsoft has stated that its Enterprise Mobility + Security (EMS) is the fastest growing product in the history of the …
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

829 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