Share via

What am I doing wrong with this powershell script?

Aisha Southern 1 Reputation point
2022-12-13T21:12:23.653+00:00

Get Members Of Information from AD

$name = Read-Host -Prompt "Please enter your name"
Get-ADPrincipalGroupMembership $name | select name

Get Members Of Information for a Group from AD

$groupmemberof = Read-Host -Prompt "Please enter the group name
Get-ADGroup -Filter 'CN -like "$groupmemberof"'-Properties MemberOf | Select-Object -ExpandProperty MemberOf

I cannot get the 'CN -like to use the variable I put in in the beginning of the script in the prompt

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 48,036 Reputation points
    2022-12-14T03:29:02.197+00:00

    The quoting used in the filter string is wrong:

    Get-ADGroup -Filter "CN -like '$groupmemberof'"-Properties MemberOf |  
        Select-Object -ExpandProperty MemberOf  
    

    If you surround the string with single-quotes you prevent the interpolation of the variable $groupname. Instead, use double-quotes around the whole string and single-quotes around the variable.

    0 comments No comments

  2. Aung Zaw Min Thwin 306 Reputation points
    2022-12-14T03:01:57.377+00:00

    Have you tried using Get-ADGroupMember?

    get-adgroupmember

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.