Retrieving SamAccountName and associated groups in a formatted csv - powershell

I'm trying to retrieve a list of all Ad users matching a filter, pipe that into Get-ADPrincipalGroupMembership and then export the result to an easy to read CSV.
*NB I can't use MemberOf as it returns blank for every single Ad user, and most successful scripts I've found are using MemberOf.
Here's what I've tried which gives me a list of groups but no association as to who goes where. Tried to export-csv as well but it complains of an empty pipe?
import-module activedirectory
foreach ($user in (Get-AdUser -Filter {(Name -Like "*(s)") } | select samaccountName)) {
Get-ADPrincipalGroupMembership $user.samaccountName | select samaccountname,name
}

This will include the user's sAMAccountName in the group results:
Import-Module ActiveDirectory
ForEach ($user in (Get-AdUser -Filter {(Name -Like "*(s)") } | select sAMAccountName)) {
Get-ADPrincipalGroupMembership $user.sAMAccountName| select #{Expression={$user.sAMAccountName};Label="User"},sAMAccountName,name
}
That weird notation is for creating a custom table. You can read more about it here: https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-powershell-1.0/ee692794(v=technet.10)
Not seeing anything in memberOf may be normal. If you check in Users and Computers, do you see a value in memberOf?
The memberOf attribute will only show groups with a Universal scope in the same AD forest, Global groups on the same domain, or Domain Local groups on the same domain of the server you're reading from (which may not be the same domain as the user). So it has its limitations.
Users can also be "a member" of a group by the primaryGroupId attribute, which stores the RID (the last section of the SID) of the user's primary group. This is usually only used for the Domain Users group.
Get-ADPrincipalGroupMembership takes care of all of that for you. It will include the primary group and search every domain in your forest for Global and Domain Local groups that have the user as a member.

Related

Get AD Group Member Groups and Find Those Group Notes in One Query

I would like to find an AD group user's group's that their in and to find what those different group's notes are.
Right now, I'm trying this
Get-ADPrincipalGroupMembership "username" | Get-ADUser -Properties info,description
Which is giving errors. I know there must be an easy way to do this that I'm missing.
I believe this is what you're looking for, query the user's MemberOf attribute and for each group, query the group's Info and Description attributes (I've also added Name so you have that reference which I believe is important to have):
(Get-ADUser "username" -Properties MemberOf).MemberOf |
Get-ADGroup -Properties Name, Info, Description |
Select-Object Name, Info, Description

Powershell: Find users with the same attribute value in Active Directory

I've been working an identity management project involving Active Directory and hit a case I can't figure out the Powershell to. Essentially we are looking at the employeeID attribute, we want to find all users that have the same value in that attribute across the whole domain. Users shouldn't have the same employeeID, so if there are two or more with the same employeeID. They need to be cleaned up.
I know Powershell could do this for me but I'm not sure what commands I would need. I've been looking Get-ADUser but nothing is jumping out at me to even get started. I essentially just want a report of all users that have the same employeeID as another user so that they can be cleaned up.
You could:
Enumerate all accounts with an employeeID value
Compare and group them based on the value using Group-Object
# Fetch all user accounts with an employeeID
$employeeAccounts = Get-ADUser -Filter 'employeeID -like "*"' -Properties employeeID
# Group them by value of employeeID attribute, keep any group with more than 1 account
$accountsByEmployeeID = $employeeAccounts |Group-Object employeeID |Where-Object Count -gt 1
foreach($ID in $accountsByEmployeeID){
# $accounts will contain a list of accounts with the same employeeID
# you could send an email, file a ticket, or disable one or more of the accounts here
$accounts = $ID.Group
}

Why won't powershell recognize Active Directory group?

I use a simple Get-ADGroup on this top level of our networks Active Directory and I get back the error "get-adgroup : cannot find an object with identity 'GroupName' under: Domain
I have tried to query the group using "Find" in Active Directory Users and Computers and it is only able to be found if set to search for Organizational Units, however if I try a Get-ADOrganizationalUnit with the name of the OU nothing will populate.
Most of the time, the group name you see from your front-end is not actually what the group name is in ActiveDirectory.
I suggest you install Active Directory Users and Computers
Query the group based on the current group name you think it is.
Open the properties and see what the actual group name is.
The fact that Get-AdGroup returned that error means you have the wrong group name.
For Get-ADGroup you have to use the objects GUID or the DistinguishedName. What I typically do with this is something like this:
Get-DistributionGroup -identity "GroupName" | select -Expand DistinguishedName | Get-ADGroup

Remove users from AD group if they do not exist in CSV

I have a task where we have a .csv file containing users. The contents of the CSV should be compared against an AD security group. Any user that is NOT in the CSV needs to be removed from the AD group.
In the past I have added/removed users from groups that ARE listed in the CSV file, but have no idea how to remove them if they are not in the CSV.
Will I have to use one script to dump the members of the group to a file, compare this file against the CSV which would then create a file of the users in the group but not in the CSV, and then use that as my source to remove the users?
Create a list from the relevant user attribute in the CSV (e.g. the distinguished name). Get the group members that are not present in this list using a Where-Object filter with the -notcontains operator. Then remove those members from the group.
Example:
$validUsers = Import-Csv 'C:\path\to\your.csv' | Select-Object -Expand dn
$invalidUsers = Get-ADGroupMember 'groupname' |
Where-Object { $validUsers -notcontains $_.distinguishedName }
Remove-ADGroupMember 'groupname' $invalidUsers -WhatIf
Remove the -WhatIf switch to actually remove the group members instead of doing a dry-run.

List groups that have members from a specific OU in ActiveDirectory

I have an OU called "InactiveUsers", to which I move all users that will be inactivated in my organization.
And I'm trying to list all the groups in my domain, having at least one user OU (InactiveUsers) as a member.
But do not know how to do, someone could help me. Thanks.
Get-ADUser -Filter * -SearchBase "<OU=InactiveUsers,etc..>" | Select -ExpandProperty Memberof | select -Unique | sort
That's the basic idea using the MS-provided AD Module.