New to powershell - Add-ADPrincipalGroupMembership help

Hi Everyone,

I’m a beginner to powershell. I need some help to better understanding the object input type.

Trying to create basic two line cmdlet for adding membership to new user and keep getting error. I know I’m doing something wrong, I just want some to help me point right direction what to look for these type of issues.

Here is my cmdlets.

creating a variable to get the user ID from Text.

$userid=Get-Content C:\PowershellTraining\Production\usercreation.txt
#now to add the user membership
Add-ADPrincipalGroupMembership -Identity $userid -MemberOf Group1, Group2, Group3, Group4

I know get-content is string object and -identity only accepts AD principal type. So that’s definitely the reason but how I get this to work. What should be looking for when I’m writing command like this and where.

please let me know

So this can be a little confusing. As you stated the documentation for this cmdlet makes it seem like it takes an AD principal type, but upon further investigation this is misleading. Here’s what the full documentation actually says:
“Specifies an Active Directory principal object by providing one of the following property values.”
Those property values are Distinguished Name, GUID, Security Identifier or SAM Account Name. All of those property values are STRINGs. So in essence this does take a string but specifically a string that represents a value of those specific properties.

Another thing to watch here is I believe it will only accept a scalar value (no collections). In your code you are using Get-Content to retrieve data from a text file and placing it in a variable. Unless you include the -Raw switch Get-Content will return an array of strings where each item in the array is a line of text in the file. So if your text file contains more than one user id, you’ll probably need to loop through them and call Add-ADGroupMembership for each item.