# Logon notification

**URL:** <https://forums.powershell.org/t/logon-notification/8447>\
**Category:** PowerShell Help\
**Created:** [April 18, 2017, 4:32pm UTC](https://forums.powershell.org/t/logon-notification/8447 "2017-04-18T16:32:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jarek-c](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@jarek-c](https://forums.powershell.org/u/jarek-c)\
**Post date:** [April 18, 2017, 4:32pm UTC](https://forums.powershell.org/t/logon-notification/8447/1 "2017-04-18T16:32:37Z")

</div>

I’d like to have an email notification on user logon to certain machines. It’s a domain environment so username would be a nice to have in the email. I can create a logon triggered scheduled task to run a script but not sure how to get the script to figure out what user logged on since $env:username would give me the user account that runs the task.

---

<div class="post-metadata">

**Author:** ![jarek-c](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@jarek-c](https://forums.powershell.org/u/jarek-c)\
**Post date:** [April 19, 2017, 7:52am UTC](https://forums.powershell.org/t/logon-notification/8447/2 "2017-04-19T07:52:04Z")

</div>

Seems like getting the right event with Get-WinEvent and turning it to XML then looking for logon type 10 and process System32 got me the account name so I could insert it into email body.

---

<div class="post-metadata">

**Author:** ![aartemjevas](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/aartemjevas/32/483_2.png) [@aartemjevas](https://forums.powershell.org/u/aartemjevas)\
**Post date:** [April 21, 2017, 7:37am UTC](https://forums.powershell.org/t/logon-notification/8447/3 "2017-04-21T07:37:22Z")

</div>

Hi JC,  
You could also try to get currently logged in users from WMI Win32\_ComputerSystem class.

```
(Get-CimInstance -class Win32_ComputerSystem).username
#If user is logged in, it will return string DOMAIN\username
```

---

<div class="post-metadata">

**Author:** ![jarek-c](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@jarek-c](https://forums.powershell.org/u/jarek-c)\
**Post date:** [April 21, 2017, 10:27am UTC](https://forums.powershell.org/t/logon-notification/8447/4 "2017-04-21T10:27:01Z")

</div>

Thanks Aleksandras. That’s a good idea too although I’m more interested in the user that just logged in rather than all users logged in.

---

<div class="post-metadata">

**Author:** ![aartemjevas](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/aartemjevas/32/483_2.png) [@aartemjevas](https://forums.powershell.org/u/aartemjevas)\
**Post date:** [April 21, 2017, 11:45am UTC](https://forums.powershell.org/t/logon-notification/8447/5 "2017-04-21T11:45:01Z")

</div>

Tried to do “Switch account” and then get username from Win32\_ComputerSystem. Looks like it return only one currently logged in user. Info from MSDN:

Data type: string  
Access type: Read-only  
Qualifiers: MappingStrings (“Win32API|System Information Functions|GetUserName”)  
Name of a user that is logged on currently. This property must have a value. In a terminal services session, UserName returns the name of the user that is logged on to the console—not the user logged on during the terminal service session.

---

<div class="post-metadata">

**Author:** ![jarek-c](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@jarek-c](https://forums.powershell.org/u/jarek-c)\
**Post date:** [April 21, 2017, 1:56pm UTC](https://forums.powershell.org/t/logon-notification/8447/6 "2017-04-21T13:56:12Z")

</div>

This works on my home PC but not on my office Server 2012R2 domain member. Doesn’t return anything.

---

<div class="post-metadata">

**Author:** ![ajdalholm](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@ajdalholm](https://forums.powershell.org/u/ajdalholm)\
**Post date:** [April 22, 2017, 12:54am UTC](https://forums.powershell.org/t/logon-notification/8447/7 "2017-04-22T00:54:38Z")

</div>

I would also choose the eventlog path… Since your only interested in “some” clients, I would setup Windows Event Forwarding Service and create a policy for the clients in question… then you can run the script from a centralized location…

---

<div class="post-metadata">

**Author:** ![aartemjevas](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/aartemjevas/32/483_2.png) [@aartemjevas](https://forums.powershell.org/u/aartemjevas)\
**Post date:** [April 24, 2017, 2:47am UTC](https://forums.powershell.org/t/logon-notification/8447/8 "2017-04-24T02:47:12Z")

</div>

It returns null if you’re logged in via RDP.

---

<div class="post-metadata">

**Author:** ![jarek-c](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@jarek-c](https://forums.powershell.org/u/jarek-c)\
**Post date:** [April 26, 2017, 2:41am UTC](https://forums.powershell.org/t/logon-notification/8447/9 "2017-04-26T02:41:06Z")

</div>

This would explain it 🙂 Many thanks

---

<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:38pm UTC](https://forums.powershell.org/t/logon-notification/8447/10 "2024-05-16T20:38:51Z")

</div>

Closed on May 16, 2024, 8:38pm UTC


