Powershell Active Directory get all users from my AD groups - powershell

I try since a while to create a csv file which contain:
"Group Name","SamAccountName"
Where GroupName is the name of the Group abd SamAccountName is the name of the user which is part of the Group.
I try this:
Get-ADUser -Filter * -Properties DisplayName,memberof | % {
$Name = $_.DisplayName
$_.memberof | Get-ADGroup | Select #{N="User";E={$Name}},Name
} | Export-Csv -NoTypeInformation -Encoding UTF8 -delimiter "," "All_Users_With_All_Their_Groups.csv"
However it doesn't work like I want.
I try to google many example but it's not pretty simple I think as I don't find some relevant example.
Do you have any idea?

This should do your work:
Import-Module ActiveDirectory ;
Get-ADGroup -Filter {name -like "*Your Group Name*"} -Properties Description,info | Select Name,samaccountname | Export-Csv D:\output.csv -NoTypeInformation
Get-ADGroupMember YourGroupName # to list members ;

I've created two ways, dunno which one You wanted
get-aduser -Filter * -Properties memberof |
%{[pscustomobject]`
#{'Groups Names'=$(($_.memberof | Get-ADGroup).name -join "," );
User=$($_.samaccountname)}}|
Export-Csv -NoTypeInformation -Encoding UTF8 -Delimiter ',' "output.csv"
Get-ADGroup -Filter * -Properties members |
%{[pscustomobject]#{'Group'=$($_.name);
'Members'=$(($_.members | Get-ADUser).samaccountname -join ",")}} |
Export-Csv -NoTypeInformation -Encoding UTF8 -Delimiter ',' "output.csv"

Related

Get-ADGroup members email address attribute

I have this code that pulls members of all distribution groups. I'm trying to get the member's email address to the same csv file as a new column instead of overwriting the DL data.
Get-ADGroup -Filter 'GroupCategory -eq "Distribution"' -Properties * | Select-Object name, mail, #{l='ManagedBy';e={$_.managedby -replace '^CN=|,.*$'}}, #{l='Members';e={$_.members -replace '^CN=|,.*$' -join ","}}, #{l='MemberOf';e={$_.memberOf -replace '^CN=|,.*$' -join ","}} | Sort-Object Name, mail, ManagedBy, Members, MemberOf | export-csv DL.csv
import-csv -Path DL.csv | ForEach-Object {
Get-ADUser -Filter {Members -like $_.members} -properties mail | Select-Object SamAccountName,Name,GivenName,mail
} | Export-csv -Path DL.csv -NoTypeInformation

Trying to export from a multivalued attribute "Proxyaddresses" to csv

Trying to export from a multivalued attribute "Proxyaddresses" from multiple OUs to csv. i am getting "Microsoft.ActiveDirectory.Management.ADPropertyValueCollection"
Here is my code,
Thanks
"ou=no gpo,ou=Staff, ou=offices,dc=ddddd,dc=ca",
"ou=Staff win10,dc=ddddd,dc=ca" | ForEach-Object {
Get-ADUser -Filter * -SearchBase $_ -Properties *
}| select name, #{Name=’proxyAddresses’;Expression={[string]::join(“;”, ($_.proxyAddresses))}} | Export-Csv c:\temp\all_proxyaddresses.csv -NoTypeInformation
Try this:
$a= "ou=no gpo,ou=Staff, ou=offices,dc=ddddd,dc=ca", "ou=Staff win10,dc=ddddd,dc=ca"
$a| ForEach-Object { Get-ADUser -Filter * -SearchBase $_ -Properties *}| select name, #{Name=’proxyAddresses’;Expression={$_.proxyAddresses -join ';'}} | Export-Csv c:\temp\all_proxyaddresses.csv -NoTypeInformation

collect samaccount powershell

I have a list of users in a CSV, but I need to collect the SamAccount attribute from each user by name in the ad.
CSV model
Script
Get-ADObject -Filter 'ObjectClass -eq "user" -and userAccountControl -eq "512"' -Properties * | Select-Object SamAccountName,CN,DisplayName, | Export-CSV -Path C:\Temp\UserAccounts.csv -Encoding UTF8 -NoTypeInformation
I'm a little lost I don't know how to do a foreach using name
I am trying but without success.
Trying to get samaccountname based on Name on csv file.
Import-Csv -Path C:\Temp\userteste.csv | foreach-Object {Get-ADUser -Filter {Name -like $_.name} -Properties Name | Select-Object samAccountName}
and export to csv file.
Why use Get-ADObject and not Get-ADUser for this? The latter gives you more of the desired properties you need in the CSV.
As aside, it is wasteful to do -Properties * if all you want is a small set of user attributes.
Something like this should work:
Get-ADUser -Filter "Enabled -eq $true" -Properties DisplayName, CN |
Select-Object SamAccountName, CN, DisplayName |
Export-Csv -Path C:\Temp\UserAccounts.csv -Encoding UTF8 -NoTypeInformation
As per your comment you need to get some extra attributes of the users listed in the CSV, you can do this:
Import-Csv -Path C:\Temp\userteste.csv | ForEach-Object {
Get-ADUser -Filter "Name -like '$($_.Name)'" -Properties DisplayName, CN |
Select-Object SamAccountName, CN, DisplayName
} | Export-Csv -Path C:\Temp\UserAccounts.csv -Encoding UTF8 -NoTypeInformation
Hope that helps

Export csv of groups that are not members of some groups

Im exporting some users and groups that "are not member of". But Im having troubles with groups, I did it with users but I don't know how to do the same with group.
In this example I export users that are not members of that group.
$groups = 'GG_LCS_UsersType4', 'GG_LCS_UsersType3', 'GG_LCS_UsersType2', 'GG_LCS_SpecialUsers'
$whereFilter = $groups | Foreach-Object {
$g = (Get-ADGroup -server $domain $_).DistinguishedName
"{0} '{1}'" -f '$_.memberOf -notcontains',$g
}
$whereFilter = [scriptblock]::Create($whereFilter -join " -and ")
$users = (Get-ADUser -server $Domain -filter {objectclass -eq "user"} -properties memberof).where($whereFilter)
$users | Select-Object SamAccountName,Enabled |
Export-Csv "${Domain}_Users_withoutGG.csv" -NoTypeInformation -Encoding UTF8 -Append
So I want the groups too. Could you help me please?
Thank you!
Technically, you should be able to just add a Get-ADGroup command at the end of the posted code and then export the desired data.
$FilteredGroups = (Get-ADGroup -Server $Domain -Filter * -Properties MemberOf).where($whereFilter)
$FilteredGroups | Select-Object SamAccountName |
Export-Csv "groups.csv" -NoTypeInformation -Encoding UTF8 -Append

Get all users in an ADgroup with the displayname

I am trying to get the members of a list of groups from a csv. The script i created below outs puts the group names. My question is : I want to get DisplayName instead of users name.
get-content "C:\temp\glist.csv" | foreach {get-adgroup -identity "$_" -properties * | select Name,Description, #{n='Members';e={$_.members -replace '^cn=([^,]+).+$','$1' -join '; '}} | export-csv "C:\temp\group_info $currentdate.csv" -NoTypeInformation -Encoding UTF8}
I'm not sure if I entirely understood which property you can to get out, but try this instead, it will get the Name property instead for each group member:
Get-Content "C:\temp\glist.csv" `
| ForEach {
Get-ADGroup -Properties * -Filter * | select Name, Description, #{n='Members';e={($_.Members | Get-ADObject -Properties Name) -replace '^cn=([^,]+).+$','$1' -join '; '}} `
} `
| export-csv "C:\temp\group_info $currentdate.csv" -NoTypeInformation -Encoding UTF8