So I'm trying to find the SID for a user that's logged onto a system before. Our system has a split of non-administrative users (without a # at the start) and administrative users (with a #). My PowerShell script so far is this:
$CurrentDomainUser = wmic computersystem get username
$Separator = "\"
$CurrentDomainUserSplit = $CurrentDomainUser.split($Separator)
$DomainUser= $CurrentDomainUserSplit[3]
New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_Users
$UserSID = ls 'hklm:software/microsoft/windows nt/currentversion/profilelist' | ? {
$_.getvalue('profileimagepath') -match '$DomainUser' -and
$_.getvalue('profileimagepath') -notmatch '#'
} | % pschildname
This script doesn't work if I have use the '$DomainUser' in the final $UserSID = ... line above. It does work if I put in the actual value that I'm searching for.
I'm guessing this is a simple PowerShell syntax problem.
$CurrentDomainUserSplit[3], why3? And what are you trying to achieve in the end ?'$DomainUser'you're using a non-expandable string so your string will literally contain$DomainUsernot the content of the variable behind. Either use double quotes ("$DomainUser") or, better still, don't use quotes at all in this case.