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 15, 2021Copper Contributor
The command is correct. What happens when you enter get-msoluser -userprincipalname username@domain.com directly without script
charlie4872
Apr 15, 2021Brass Contributor
When I use get-msoluser -userprincipalname user@domain.com | select-object LastPasswordChangeTimestamp by itself I get the output with just the date of the last password change as expected.
When I adjust the commands as you suggested there is no error now (improvement) but it only returns the password change time for the Get-Aduser portion of the script and nothing from the get-msoluser portion of it. Very strange. Thanks again for your help.
When I adjust the commands as you suggested there is no error now (improvement) but it only returns the password change time for the Get-Aduser portion of the script and nothing from the get-msoluser portion of it. Very strange. Thanks again for your help.
- JaksaSkelinApr 16, 2021Copper Contributor
Can you output the content of the $UPN variable, maybe like this:
$user = read-host 'Enter Username' $UPN = get-aduser -identity $user | select-object -expand UserPrincipalName get-aduser $user -properties * |select PasswordLastSet Write-Host "[$UPN]" get-msoluser -userprincipalname $UPN |select LastPasswordChangeTimestamp
- charlie4872Apr 16, 2021Brass ContributorThanks for the reply JaksaSkelin. Using that I get the same results only this time it writes the UPN to the screen like below but no LastPasswordChangeTimestamp results. Here is what comes back
username@domain.com
PasswordLastSet
------------------
2/17/2020 8:05:09 AM
So only the AD user password details get displayed not the password details from mosoluser. No errors though so I cant tell what the issue may be.
Thanks again!- JaksaSkelinApr 17, 2021Copper Contributorwhat is the output from
get-msoluser -userprincipalname user@domain.com | fl