# Help with variable

**URL:** <https://forums.powershell.org/t/help-with-variable/22170>\
**Category:** PowerShell Help\
**Created:** [April 19, 2023, 4:21pm UTC](https://forums.powershell.org/t/help-with-variable/22170 "2023-04-19T16:21:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pxbxy](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@pxbxy](https://forums.powershell.org/u/pxbxy)\
**Post date:** [April 19, 2023, 4:21pm UTC](https://forums.powershell.org/t/help-with-variable/22170/1 "2023-04-19T16:21:19Z")

</div>

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 …
- 

```auto
  + 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 …
- 

```auto
  + 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](mailto: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?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 19, 2023, 4:55pm UTC](https://forums.powershell.org/t/help-with-variable/22170/2 "2023-04-19T16:55:54Z")

</div>

pxbxy,  
Welcome to the forum. 👋🏼

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](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) 1 \<---- Click 👆🏽 😉

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

```auto
$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:

```auto
$ADUser.GivenName
$ADUser.SurName
$ADUser.UserPrincipalName

```

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

```auto
Remove-MsolUser -UserPrincipalName $ADUser.UserPrincipalName -RemoveFromRecycleBin

```

---

<div class="post-metadata">

**Author:** ![pxbxy](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@pxbxy](https://forums.powershell.org/u/pxbxy)\
**Post date:** [April 19, 2023, 7:48pm UTC](https://forums.powershell.org/t/help-with-variable/22170/3 "2023-04-19T19:48:13Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:14pm UTC](https://forums.powershell.org/t/help-with-variable/22170/4 "2024-05-16T20:14:58Z")

</div>


