0

Currently I want to get a list of active user on Windows 10 using Powershell.
After some searching I found this helps:

Get-LocalUser | Where-Object -Property Enabled -eq True

And here is the output:

Name       Enabled Description
----       ------- -----------
User_1     True               
User_2     True

I just want to concatenate strings to a list of values of Name column from above, which will be like below:

Active user(s): User_1, User_2

Do you have any idea how I can do that? I'm just a non-tech guy trying to learn some useful command so forgive me if this is a basic to you.

1 Answer 1

1

You can do the following:

# Retrieve the name values
$Users = Get-LocalUser |
    Where-Object Enabled -eq True | Select-Object -Expand Name
# Create the output string
"Active user(s): {0}" -f ($Users -join ', ')

Using -Expand (or -ExpandProperty) from Select-Object, the target property's value is returned rather than the object that contains the property.

-f is the string format operator. It uses substitution for the {position} syntax. The -join operator creates a string from a list with , as the delimiter.

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

3 Comments

So can I count the number of active user? Just for curious.
$users.count for the count
It works like a charm XD Really appreciate your help.

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.