Groups within a certain OU that a user is part of - powershell

I have the name of a user, and I need to find all the groups that this user is a part of - but only those groups which are within a certain OU.
How do I do this? I know that: Get-ADPrincipalGroupMembership cmdlet
finds all the groups that the user is a part of, but I have no idea how to filter this down to the specific OU, as none of the options seem to be helping.
Alternatively, any other way of doing this would be welcome.

Use a Where-Object for the DistinguishedName Path
Get-ADPrincipalGroupMembership -Identity user |
Where-Object {$_.DistinguishedName -match 'OU=SubOU,OU=MainOU,DC=Domain,DC=local'}

Related

How to disabled Group in AD

I have simple requirement to disable/deactivate group in AD. There are plenty of options for AD users & Computers but did not see anything related to groups.
Basically, We want to remove all member from the group and set group to InActive or disabled. I can think of below approach but not sure if it is right way to do it.
Remove-ADGroup is not option in our case due to some security and audit concerns.
Remove all members from the group and move group to non-operational OU
Remove all members for the group and set enable flag to "false"
Please suggest best way/solution to achieve this.
Thanks
This command will remove an AD Group for you.
Remove-ADGroup
This should help you
Get-ADGroup will get all the groups,
Get-ADGroupMember will get all the member then Move-ADObject will move the group to another OU
$AdGroups = Get-ADGroup -filter * | Select-Object -ExpandProperty Name
foreach($ADgroup in $ADgroups){
Get-ADGroupMember "$ADgroup" | ForEach-Object {Remove-ADGroupMember "$ADgroup" $_ -Confirm:$false}
Move-ADObject -Identity $AGroup -TargetPath "OU=disable,DC=test,DC=local"
}

I need a more specific Powershell command to isolate Active Directory names, usernames, and last login date from users on a domain

I'm an IT intern tasked with performing an audit of users on our domain and I'm having some trouble finding the info I need without all of the extra stuff. Is there a way to pull all of this info in one command? If not, can you recommend commands to pull users, usernames, and login info separately in a manner that I can copy-paste in the format I need?
I previously used get-adgroup -filter * and wrote to a file. Are there some options I can add for this filter? I also used a script to get all users, and all groups and their user permissions on separate occasions.
You could try something like:
Get-ADGroup -Filter "Name -like '*Accounting*'" |
Get-ADGroupMember |
Select-Object name, SamAccountName
Or if you need more fields from the user object, then try something like:
Get-ADGroup -Filter "Name -like '*Accounting*'" |
Get-ADGroupMember |
Get-ADUser -Properties Enabled |
Select-Object Name, SamAccountName, UserPrincipalName, Enabled
You'll probably want to export to a spreadsheet, so use Export-Csv for that.

Copygroup membership from one group to another powershell

I was hoping someone can point me in the right direction please.
Im trying to do something that should be pretty straight forward i think, but i can;t get it to work or can i find any similar examples. basically, i want to be able to do the following:
Look at an existing Security Group 'Member of' groups and then add those member of groups to another/new group. So for example, group 1 is member of 'A, B, C' groups. Group 2 is memebr of none. I want to copy the membership of Group 1 to Group 2, but NOT users (although if that was a must they could then be removed easily enough.
What i dont need to worry about is any users, or copying groups that users are members of etc.
Thanks
Thanks for that... so if i look at something like:
Add-ADGroupMember -Identity 'TARGETGROUP' -Members (Get-ADGroupMember -Identity 'SOURCEGROUP' -Recursive -Server Server1) -Server Server1
This adds users from Sourcegroup to Targetgroup, but im not worried about users, its the sourcegroups 'Member of' details i want addding to the targetgroup if that makes sense?
I can extract the info from the targetgroup using something like
$Groups = Get-ADGroup -Identity 'SourceGroup' -Properties memberof -Server Server1 | select MemberOf | Format-Table -AutoSize -Wrap
But then cant seem to do much with importing that info into the new group. Hope that makes sense? :)
Thanks for the info: That looks to be trying to add the groups from Source into the Members of section as oppose to the member of section for the groups, if that makes sense? it states 'Add-ADGroupMember : A universal group cannot have a local group as a member' which would suggest its tring to add the groups a s amember of the new group, not into the 'member of' of the new group..
Just wanted to share the following (in crude form) as this doen what i was after:
Get-ADGroup -Identity %SOURCE% -Properties memberof -Server SERVER1 |
Select-Object -ExpandProperty memberof |
Add-ADGroupMember -Members %TARGET% -Server SERVER1
Thanks for your help chaps.

How to get list of groups with no ManageBy from a specific OU in Active Directory with Powershell

Hopefully someone can help me out. I am trying to get into a specific Organizational Unit which contains multiple groups and I want to display the ones that has a blank ManageBy field. The problem I don't know how to overcome mostly is how to get all the groups out of the OU I don't need or want the actual users of the groups just the groups the name of the groups and the ones without a ManageBy field from that OU. I know how to get groups and show their names by doing.
Get-ADGroup -Filter 'Names "*"'
This would just list all the groups in the whole Active Directory not just the specific OU and I don't know how to filter only the ones that have a blank ManageBy field. The only thing I can think of for getting the groups with no ManagedBy is this
Get-ADGroups | Where-Object {$_.ManagedBy -eq $null)
The only other thing I could think of was to create a variable and assign it a specific OU and then get the groups from that variable.
But I don't know if that's even possible to work. I am really new to PowerShell and Active Directory so any help would be great. If someone could help me out with this I would appreciate it.
If you're going to use Where-Object, you need to ask Get-ADGroup to return the ManagedBy property by using the -Properties parameter. Otherwise, it'll always be null.
Get-ADGroup -Filter * -Properties ManagedBy -SearchBase "OU=My OU,DC=TacoTruck,DC=org" | Where-Object {$_.ManagedBy -eq $null}
However, if you pipe the results into Where-Object, you are asking AD for more than you need. You're getting every group in the OU, then you're discarding some of the results. It'll work, it's just unnecessary traffic.
This will ask AD for only what you need:
Get-ADGroup -LDAPFilter "(!managedBy=*)" -SearchBase "OU=My OU,DC=TacoTruck,DC=org"
Performing a simple Get-Help Get-ADGroup -Full (or going to this link) would probably give you the answer you're looking for, but for the sake of others possibly wanting this information you can use the -SearchBase parameter to specify an OU to search.
Get-ADGroup -Filter * -SearchBase 'OU=My OU,DC=TacoTruck,DC=org'
If you do not want to include any child OU's then you would also want to specify -SearchScope 0.

Powershell Script to search specific OU in AD and find disabled users that is member of a group

I'm trying to write a script to find disabled users that is member of one or more groups in a specific OU in AD. It will then remove all the groups for all the disabled users. I found this script which removes all groups from users in a csv file, but as i'm looking to run this as a scheduled task I prefer not to process users that already had their groups removed without having to move them to a different OU.
Import-Csv $csvFile | ForEach-Object {
# Disable the account
Disable-ADAccount -Identity $_.samAccountName
# Retrieve the user object and MemberOf property
$user = Get-ADUser -Identity $_.samAccountName -Properties MemberOf
# Remove all group memberships (will leave Domain Users as this is NOT in the MemberOf property returned by Get-ADUser)
foreach ($group in ($user | Select-Object -ExpandProperty MemberOf))
{
Remove-ADGroupMember -Identity $group -Members $user -Confirm:$false
}
}
Any idea on how to filter out the users with more then one group?
I'm using this script to export disabled users that has not logged on for 60 days:
Get-QADUser -searchRoot $OuDomain -searchScope OneLevel -InactiveFor 61 -NotLoggedOnFor 61 -disabled -sizelimit 0
Thx
You seem to have filter by ou part down which is good. You have some thoughts in the beginning of you post but the only actual question is how to filter out the users with more then one group. Not sure if that is a typo or not but I read that as checking the count of groups a user has. A more realistic interpretation of that is filter users that could have at least one of a list of groups. I'm going to cover both.
The Count
I'm sure this is not what you want but just want to cover the base. The following would also work in a Where-Object clause
If((get-aduser $user -Properties MemberOf).MemberOf.Count -gt 0){Process...}
Multiple Groups
I'm sure this was your intention. Locate users that could contain one of serveral groups. This is best handled with regex.
$groupsFilter = "citrix_GateKeeper","barracuda_spam_alerts"
$groupsFilter = "($($groupsFilter -join '|'))"
# $groupsFilter in this example is: (citrix_GateKeeper|barracuda_spam_alerts)
If(((Get-ADUser $user -Properties MemberOf).MemberOf) -match $groupsFilter){Process....}
Create a regex match string based on a string array of multiple groups. If $user is a member of either of those groups then true would be returned.
If nothing here is of any use to you then I would suggest making your question clearer. Hopefully this helps.