Forum Discussion
charlie4872
Apr 14, 2021Brass Contributor
Get Userprincipalname to use as second variable
Hello I am trying to use get-aduser and then extract the userprincipalname for use in a second command in a PS script. However it is not working as expected. Can anyone help as to why this is not wor...
JaksaSkelin
Apr 14, 2021Copper Contributor
charlie4872
The command $UPN = get-aduser -identity $user | select-object UserPrincipalName does not return a string, it returns an object with the propery UserPrincipalName. You need to adjust the second call as follows: get-msoluser -userprincipalname $UPN.UserPrincipalName | select LastPasswordChangeTimestamp
Alternatively you can also fetch UserPrincipalName as string: $UPN = get-aduser -identity $user | select-object -expand UserPrincipalName
- charlie4872Apr 15, 2021Brass ContributorHello JaksaSkelin thanks for the response! I have tried what you suggested and although I don't get an error in PS now, it appears to only returning just the results of the "PasswordLastSet" from the Get-Aduser and not the results of the Get-Msoluser "LastPasswordChangeTimestamp". Any idea why the actual results of the Get-Msoluser command don't appear?
Thanks!