Help with variable

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?

pxbxy,
Welcome to the forum. :wave:t3:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

Your code is unnecessarily convoluted and repetitive. You query the AD 4 times for the same user.
All you need is this:

$sAMAccountName = Read-Host 'What is the username you would like to delete?'
$ADUser = Get-ADUser -Identity $sAMAccountName 

Since GivenName, SurName and UserPrincipalName belong to the default set of properties Get-ADUser returns … by default … you don’t need to use -Properties *. In fact you should never use it with an asterisk (*). Always provide the properties you’re really after. Everything else is putting unnecessary stress to your DC.

Now that you have the user object in the variable $ADUser you can access its properties with the so colled dot notation … like this:

$ADUser.GivenName
$ADUser.SurName
$ADUser.UserPrincipalName

And this is what you should use to delete the account:

Remove-MsolUser -UserPrincipalName $ADUser.UserPrincipalName -RemoveFromRecycleBin
1 Like

Thanks Olaf! This makes perfect sense. Thanks for explaining it in words a newbie like me would understand and thanks for welcoming me. I’m glad to be hear!