2

I am trying to return simply the active username to use later in a script, however I am currently hung up on having my variable populate only the username, excluding the domain.

Function Get-Username() {
$username = Get-WMIObject -class Win32_ComputerSystem | select username

return $username
}


Function Delimit-Username() {
$newUserName = $username -replace 'DOMAIN\',''

Write-Host $newUserName
}

Get-Username
Delimit-Username
1
  • 1
    $Domain,$User = (Get-WMIObject -class Win32_ComputerSystem).UserName.Split('\') Commented Aug 13, 2018 at 20:15

2 Answers 2

1

The domain is delimited from the user by a \, hence you can use the -split operator and access the second element to get the username without the domain.

('DOMAIN\User' -split '\\')[1]

You can also access the environment variable username ($env:username)

Sign up to request clarification or add additional context in comments.

Comments

0

The USERNAME is also in the environment.

$Env:USERNAME

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.