...Because someone's gotta tell the story...


To return to the main Blog List, click Full Blog Listing.



PowerShell: Determining the Current User's SID

Thursday, April 18, 2013 in Powershell (Views: 3214)
A quickie this morning, for those deploying software to a user, but are running in the SYSTEM context. How do you do this?

First, WMI to the rescue. The win32_computersystem namespace has the info you need to tell you who is currently logged in. Of course, in a Terminal Server/Remote Desktop environment, this won't work, but this is more meant for the typical workstation/server deployment.

I actually saw the need for this while attempting to install Microsoft Research's NoReplyAll plugin for Outlook. Obviously, you only want certain users to have this, and not everyone on a system.

Here is the PowerShell to make this happen, to determine the SID of the current user, so you can drop registry keys or other items in the HKU hive...

#Logged in as systenm, find the locally logged on user anyway, strip domain...
$CS = Gwmi Win32_ComputerSystem -Comp "."
$objUser = New-Object System.Security.Principal.NTAccount($CS.UserName)
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
$RegSID = $strSID.Value

Happy Scripting...


Related Blogs You May Be Interested In:

To leave a comment, please log in and/or register.