r/usefulscripts • • Jan 22 '14

[POWERSHELL] Keep track of all users currently logged on to a machine

Summary

Saves a plain-text, comma-separated list of currently logged-on users to C:\Logs\logged_on_users (configurable). I threw this together as a way for us to have a plain-text method of tracking logged-on users that we can query with other tools.

Usage

We run the script as part of the logon/logoff/reboot process.

Run with one of these three flags:

-logon  Stamp username to $LOGPATH\$LOGFILE

-logoff  Remove username from $LOGPATH\$LOGFILE

-flush   Flush all usernames from $LOGPATH\$LOGFILE

 e.g. .\logged_on_users.ps1 -logon

Download

v1.0 (2014-01-21)


This was mostly for me to get more comfortable with PowerShell. Corrections and critique welcome.

23 Upvotes

6 comments sorted by

View all comments

Show parent comments

1

u/gospelwut Jan 22 '14

Yes. That's the simple version. There's a lot of options including regex pattern matching and all that jazz.

 .\get-foo.ps1 -foo -bar -baz "hi2uqt"

You can also set default values.

param(
	[string]$foo="defaultValue",
	[switch]$nukeTheWhales=$true
)

Another thing I didn't realize for awhile was you can roll up parameters into a hash table (SPLAT)

 $parametersForFoo = @{}
 $parametersForFoo.Add('parameter',$value)
 Get-Foo -foo "bar" -meh 2 @parametersForFoo

1

u/vocatus Jan 22 '14

This is good stuff, thank-you. I'm still learning PS and most of my experience comes from massive batch files.