Powershell to tally up departments in a file - powershell

To be honest up front I really have no idea how to start this but here is what I am trying to accomplish.
I have a csv file that has a list of usernames. What I am trying to do it perform a lookup of the username, get the department and as I parse through the csv tally up the number of times it is found.
For example
Human resources 2
information Technology 5
As previously stated I am not sure how to even approach this other than I know I will need to parse the departments first then go into the number count.
Import-Module ActiveDirectory
Import-CSV "C:\Input.csv" | % {Get-ADUser $_.LoginID -Properties * | Select -Property Department} | Export-CSV "C:\ADUsers.csv" -NoTypeInformation -Encoding UTF8

Use the Group-Object cmdlet to group users by the Department attribute value:
Import-CSV "C:\Input.csv" |ForEach-Object {
Get-ADUser $_.LoginID -Properties Department
} |Group-Object Department -NoElement |Select Name,Count |Export-Csv C:\output.csv

Related

Output on CSV and argument in powershell are not same

I got user information from the user group in AD. every column has no problem except the user name.
On csv, User name is normal but there is a format when I get content from csv for using powershell like as below;
#{Name=abc}
for compare-object with two CSV, I need to use -expand.
Is there anyway to avoid this result?
I want to get a same content on CSV and powershell.
get-adgroup $path -server server.com | get-adgroupmember -recursive | select-object -unique | get-aduser -properties mail | name, mail | export-csv c:\result.csv
Use import-csv cmdlet to import the csv and not get-content. Also the provided code sample won't work - e.g. you missed select-object here:
| name, mail |
You do not need to query the group, as you already know the name ($path), you can directly query the groupmemberships, e.g.:
get-adgroupmember -identity $path -recursive
But in the end you could achieve the same in a much more efficient way, e.g.:
get-aduser -LDAPFilter "(memberOf:1.2.840.113556.1.4.1941:=[groupDistinguishedName])" -property mail | select-object -property mail,name | export-csv [path]
replace [groupDistinguishedName] with the distinnguishedName of the group. This will give you all users back which are member (transitive) of the defined group.
see: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-adts/4e638665-f466-4597-93c4-12f2ebfabab5

Export Groups and Member SamAccountName or DisplayName to CSV

I'm having some issues importing group and membership data from the CSV I had created, the reason is because the export I am doing is exporting the member's CN name instead of the SamAccountName or DisplayName that the import requires.
Currently my exported CSV looks like this:
"name","Members"
"GROUP1","CN=LEEROY JENKINS,OU=ADMINISTRATORS,OU=USERS,OU=DOMAIN,DC=DOMAIN,DC=LOCAL;CN=MICHAEL JACKSON,OU=ADMINISTRATORS,OU=USERS,OU=DOMAIN,DC=DOMAIN,DC=LOCAL;CN=JERRY SPRINGER,OU=GUESTS,OU=USERS,OU=DOMAIN,DC=DOMAIN,DC=LOCAL"
"GROUP2","CN=KIMMY SHMIDT,OU=ADMINISTRATORS,OU=USERS,OU=DOMAIN,DC=DOMAIN,DC=LOCAL;CN=MICHAEL JACKSON,OU=ADMINISTRATORS,OU=USERS,OU=DOMAIN,DC=DOMAIN,DC=LOCAL;CN=JERRY SPRINGER,OU=GUESTS,OU=USERS,OU=DOMAIN,DC=DOMAIN,DC=LOCAL"
Which I got from executing:
Get-ADGroup -SearchBase "ou=groups,ou=DOMAIN,dc=DOMAIN,dc=local" -Properties name,members -Filter * |
select members, name |
Export-Csv BLAH.CSV -NoTypeInformation
I think I am left with two issues, one being that the import won't take the CN as a valid member name and also not sure whether it will work with each group having multiple users.
On a side note - I found this article. Similar issue, however, I haven't got as far as he has with the nicely formatted table of 'Group1 - Name1'. I'm basically trying to figure out an automatic way to create a table with the groupname and membership details that can be imported into Active Directory.
I've run into the same problem, and for me it was because groups have multiple members, and PowerShell doesn't handle the 1:M relationship well. I started using this to pull group members:
(Get-ADGroupMember -Identity GroupName -Recursive | select name | Out-String).Trim()
As that puts all of the group members into one cell. Note that Excel has a size limit on the number of characters in a single cell, so if you have a group with thousands of members, it will error out on you. I'm sure that there are better ways, that is just what works for me.
You could pipe your current script into it, ie:
Edited to more closely match what you need:
$Groups = Get-ADGroup -Filter {YOURFILTER} | select -ExpandProperty sAmAccountname
foreach($Group in $Groups)
{
$Members = (Get-ADGroupMember -Identity $Group | select sAmAccountName | out-string).Trim()
New-Object -TypeName PSCustomObject -Property #{
Users = $Members
Name = $Group
} | Export-Csv C:\Export.csv
}
Just make sure to expand the cells vertically to actually see the group members.

Powershell script to display all Users in a Group AD

I have created the below
Get-ADGroup -Filter * -SearchBase "DC=Domain,dc=.com" -properties name, members |
Select-Object *,#{Name='Member';Expression={$_.Members -replace '^CN=([^,]+).+$','$1'}} |
FT Name, Member -Autosize |
out-file c:\text.txt
Ignore Domain and .com I have them populated with my relevant information, but for sake of here removed them.
When I run this it returns what I'm after but when looking at the members within the group they all end with ... and don't show all the members
There are a few things to correct. Let's look at them in order. The actual AD query can be simplified: you only need to specify 'Members' as an additional property to retrieve as 'Name' is brought back by default:
Get-ADGroup -Filter * -SearchBase "DC=Domain,dc=.com" -properties members
Given that you only want to output two properties ('Name' and your custom one 'Member'), use your select to retrieve only the ones you want:
Select-Object Name ,#{Name='Member';Expression={$_.Members -replace '^CN=([^,]+).+$','$1'}}
Remove the Format-Table: we have already limited the selection in the previous command. Format cmdlets are designed to format the output to the console window and best practice dictates that they should only be used for that purpose and that they should always be the last element of a pipeline.
Piping all of that to Export-Csv will then produce what you want:
Export-Csv -NoTypeInformation -Path C:\text.csv
This one did the trick for me
Get-ADGroupMember -Identity Administrators | Select-Object name, objectClass,distinguishedName | Export-CSV -Path “adgroupmembers.csv”
I got this here.
https://www.lepide.com/how-to/export-members-of-a-particular-ad-group-using-poweshell.html#:~:text=The%20PowerShell%20Get%2DADGroupMember%20cmdlet,group%20you%20want%20to%20use.

Powershell script:how to Filter on AD Get commands to return certain information

I am trying to create a powershell script that checks AD group membership/domain/manage by etc amongst other information and puts into csv file, because I want to structure the csv file in a certain way how do i filter within the actual script to only return certain information e.g. withe the code below its returning a lot more columns but from these the only ones i want are "managed by" and "name":
Get-ADDomain -property managed By, Name|Export-csv -path C:\AD\Domain.csv -NoTypeInformation
Use -Properties to specify the properties you want, and then pipe to Select-Object to select the properties you want in the output. For example:
get-aduser -filter * -properties canonicalName,userPrincipalName |
select-object canonicalName,userPrincipalName |
export-csv myfile.csv -notypeinformation
Get-ADDomain doesn't have -Properties but you can still use Select-Object:
get-addomain | select-object ManagedBy,Name | export-csv myfile.csv -notypeinformation

Querying the ManagedBy attribute in PowerShell for AD

I have a small script in powershell written to query user groups in a specific OU in AD to get the name of those groups and to also try and get the ManagedBy attribute of those groups. I've been searching online and here for solutions to why the ManagedBy attribute is not populated results but I have had no luck. Every solution I have found has been written in C# (or another language) and I have tried using the Quest software for AD which doesn't seem to help.
$test = 'OU=example,DC=example,DC=test'
$test | ForEach {Get-ADGroup -Filter * -Properties ManagedBy -SearchBase $_ } | Select Name, Properties | Sort -Property Name | Out-File C:\test.csv
I am only getting results of the name of the groups and empty brackets for the ManagedBy attribute. My question is, is there anyway to query the managedby attribute in powershell without using another language or integrating different plugins? I've never written in C and I would prefer using native powershell if possible.
You've got an error in your Select. Properties should be ManagedBy.
$test = 'OU=example,DC=example,DC=test'
$test | ForEach {Get-ADGroup -Filter * -Properties ManagedBy -SearchBase $_ } |
Select Name, ManagedBy |
Sort -Property Name |
Out-File C:\test.csv