At the beginning of my script I’m trying to get the users first name, last name, username, and email and store each one into a variable and this works fine.
$GetUsername = Read-Host “What is the username you would like to delete?”
$GetSamAccountName = Get-ADUser -Filter “SamAccountName -eq ‘$GetUsername’” -Properties * | Select-Object SamAccountName
$GetGivenName = Get-ADUser -Filter “SamAccountName -eq ‘$GetUsername’” -Properties * | Select-Object GivenName
$GetSurname = Get-ADUser -Filter “SamAccountName -eq ‘$GetUsername’” -Properties * | Select-Object SurName
$GetUserprincipalname = Get-ADUser -Filter “SamAccountName -eq ‘$GetUsername’” -Properties * | Select-Object UserPrincipalName
In between my script I’m able to delete the AD account, delete the users home folder, then run a AAD Connect sync (hybrid environment). At the end of my script I try to delete the account out of M365’s recycling bin by using this.
Remove-MsolUser -UserPrincipalName $GetUserprincipalname -RemoveFromRecycleBin
But I get this error
Remove-MsolUser : User Not Found in the Microsoft Online directory Deleted Users container. User: @{UserPrincipalName=john.doe@domain.com.}
At line:19 char:1
- Remove-MsolUser -UserPrincipalName $GetUserprincipalname -RemoveFromR …
-
+ CategoryInfo : OperationStopped: (:) [Remove-MsolUser], MicrosoftOnlineException + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.UserNotFoundException,Microsoft.Online.Administration.Automation.RemoveUserRemove-MsolUser : User Not Found in the Microsoft Online directory Deleted Users container. User: @{UserPrincipalName=john.doe@domain.com}.
At line:19 char:1
- Remove-MsolUser -UserPrincipalName $GetUserprincipalname -RemoveFromR …
-
+ CategoryInfo : OperationStopped: (:) [Remove-MsolUser], MicrosoftOnlineException + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.UserNotFoundException,Microsoft.Online.Administration.Automation.RemoveUser
I see why I’m getting the error. It’s trying to look for @{UserPrincipalName=john.doe@domain.com but I was hoping it would look for john.doe@domain.com. What’s the right/better way of getting the users info and storing it as a variable so that I can use it throughout the script?