Tuesday, 11 August 2015

Powershell - Get specified user's SID and local home directory.

Get specified user's SID and local home directory.




# Get current user's samaccountname only (without the domain prefix) by checking the username on console instead of the account #running the powershell commands.






$explorerInstance = Get-WmiObject -Class Win32_Process | Where-Object -Property name -EQ -Value explorer.exe
$ownerinfo = $explorerInstance.GetOwner()
$username = $ownerinfo.User
$domaininfo = $ownerinfo.Domain
 
 
$user = New-Object System.Security.Principal.NTAccount("$domaininfo", "$username")
$sid = $user.Translate([System.Security.Principal.SecurityIdentifier])
$userprofile = Get-WmiObject -Namespace root/cimv2 -Class win32_userprofile -Filter "SID='$($sid.value)'"
$USER_HOME = $userprofile.localpath

No comments:

Post a Comment

Detect autopilot session

  Ensuring that some apps only install during autopilot is not easily accomplished, you can use the below powershell script as a requiremen...