1

I am Using this command to get a Local User in Administrators group, and it works

Get-WmiObject -Class Win32_Groupuser -ComputerName $computer |? {$_.groupcomponent -like '*"Administrators"' -and $_.Partcomponent -like '*"User"'}|ft groupcomponent,Partcomponent

But now i want to use variable like this $Group="Administrators" $Account="users"

Get-WmiObject -Class Win32_Groupuser -ComputerName $computer |? {$_.groupcomponent -like *$Group -and $_.Partcomponent -like *$Account}|ft groupcomponent,Partcomponent

but i get nothing back

 

2 Answers 2

2

If the group name is "Administrators" you do not need to use a wildcard. The asterisk will match one or more characters. For example:

"Administrator*" matches Administrator, Administators, "Administrator Groups", etc

"*Administrators" matches Administrators, "Windows Administrators", etc

"*Administrator*" matches Administrators, "Administrator Groups", "Windows Administrators", etc

For the second command you can use a sub-expression to expand the variable.

Where-Object { $_.groupcomponent -like "*$($Group)*" }
Sign up to request clarification or add additional context in comments.

Comments

1

This is a working example:

$Group = '"Administrators"'
Get-WmiObject -Class Win32_Groupuser -ComputerName $computer |? {$_.groupcomponent -like "*$Group"}

The same can be adapted for $_.partcomponent.

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.