# Script in Active Directory to get User's email

**URL:** <https://forums.powershell.org/t/script-in-active-directory-to-get-users-email/23757>\
**Category:** PowerShell Help\
**Created:** [March 12, 2024, 7:30am UTC](https://forums.powershell.org/t/script-in-active-directory-to-get-users-email/23757 "2024-03-12T07:30:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jawwad](https://avatars.discourse-cdn.com/v4/letter/j/ecb155/32.png) [@Jawwad](https://forums.powershell.org/u/Jawwad)\
**Post date:** [March 12, 2024, 7:30am UTC](https://forums.powershell.org/t/script-in-active-directory-to-get-users-email/23757/1 "2024-03-12T07:30:12Z")

</div>

Hi PS community,  
I am new and Its my first topic here.  
I am running this script to **get the email of a logged in user**  
Result are:

1. On test user, it gives result. i.e. write email adress of the logged in user to the specified location.
2. In actual, when real user logged in, file is empty.  
I have attached log file.  
Kindly help in this regards

powershell

```auto
# Retrieve the email address of the currently logged-in user
$usersearch = [adsisearcher]"(samaccountname=$env:USERNAME)"
$email = $usersearch.FindOne().Properties.mail

# Specify the path where you want to store the email address
$outputFilePath = "$env:APPDATA\cuser_swye360.dat"

# Write the email address to the specified file
$email | Out-File -FilePath $outputFilePath -Force

```

 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/6/68883393e970c725111989093e7f87c49236b2e7.png)

Regards

---

<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:** [March 12, 2024, 8:04am UTC](https://forums.powershell.org/t/script-in-active-directory-to-get-users-email/23757/2 "2024-03-12T08:04:26Z")

</div>

Jawwad,  
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 👆🏽 😉

( !! Sometimes the _preformatted text_ button hides behind the settings gear symbol. 😉 )

Since your script obviously works (you tested it) the issue might be the way you run it. It looks like you’re using a kind of systems management solution. I’d try to look for the issue in that direction.  
For example: does the impersonation of the logged on user work properly? You could write the `$env:USERNAME` to the output file as well to check that.

---

<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:** [March 13, 2024, 5:22am UTC](https://forums.powershell.org/t/script-in-active-directory-to-get-users-email/23757/3 "2024-03-13T05:22:07Z")

</div>

any reason you’re using the adsisearcher? Also how is this being called? a logon script or what exactly? Are you sure it’s running in the context of the logged in user?

I can’t reproduce your error, and it seems like you also had a test user worked. It might be worth double checking that the actual user definitely has a mail property. Next I’d run it and make sure that data is in PS prior to the export. I’m sure it is but doesn’t hurt to double check. Confirm it against Get-ADUser as well.

If posible, I’d suggest using `Get-ADUser` which has a property called ‘mail’ that is returned: [Get-ADUser (ActiveDirectory) | Microsoft Learn](https://learn.microsoft.com/en-us/powershell/module/activedirectory/get-aduser?view=windowsserver2022-ps) .

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [April 12, 2024, 5:22am UTC](https://forums.powershell.org/t/script-in-active-directory-to-get-users-email/23757/4 "2024-04-12T05:22:12Z")

</div>

Closed on April 12, 2024, 5:22am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
