Bite Size Bytes

Small coding solutions to big problems


Who’s really running that Powershell instance?

I’ve been writing a lot of custom applications lately, many using Powershell and then packaged using Sapien’s Powershell Studio. Something that I’ve had to do a few times lately is provide applications to allow a non-privileged user to manipulate a user account in a different domain. There are a few pieces that go into this, including mounting AD: as a provider and then “cd” to that “drive”. To make all of this context changing work, it requires embedding the RunAs command into the executable to allow the script to run as an admin account from the domain being affected. The problem with that Powershell can not read the current true username from $env:username. Since it is important to log who is actually making changes to another persons account, this is a problem.

WMI to the rescue. Since Powershell can read WMI, it becomes easy to store the true username in a variable and write it out to a log file.

$trueUserName = $(Get-WMIObject -class Win32_ComputerSystem | Select username).username



Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About Me

As a seasoned IT professional, I have developed an extremely well-rounded breadth and depth of knowledge and an exceptional ability to decipher and communicate complex issues. I have years of proven experience in IT leadership roles, security auditing, digital forensics, and overseeing development of custom applications, rounded out with a Bachelor of Science degree in Information Technology emphasizing Software Development. Over the course of my career, I have successfully held certifications as a GIAC Certified Incident Handler (GCIH), as well as CIW, CompTIA, and assorted Microsoft programming and networking certs. I have presented at the 2019 CETPA Conference in Anaheim and the 2021 CITE Conference in Sacramento.

Codementor badge