Powershell Script to Pull Various Data Fields in Active Directory - powershell

This is what I currently have to pull a specific facility list of all departments. What I now need to discover is the various Job Titles they have for this facility, the tricky part is that the Titles are not listed within the attributes for the user accounts within active directory. The titles are only listed under Organization tab -> Job Title. Simply adding "title" into the code does not work since that field is left blank where the script is trying to pull it from, I just need to redirect it to pull from the Job Title field under the Organization tab.
Get-ADUSER -LDAPFilter "(extensionattribute7=)"-properties department | select-object name,department -unique | Sort-object department |
Select Department -unique | Export-Csv -NoType MyCSVfile.csv

If you add Title to the -Properties parameter, it will be returned by Get-ADUser. But if your Select-Object statements don't include it, it will be discarded.
You may want to do this in multiple statements instead of a single pipeline.
$users = Get-ADUSER -LDAPFilter "(extensionattribute7=)"-properties department,title
$depts = $users | Select-Object Department -Unique
$titles = $users | Select-Object Title -Unique

Related

I have a list of Display names that I would like to also display SAM Account Names

I have an application that has never had old users cleaned out of it. I exported all the LastName, FirstName to a .CSV, but would like to have it add the SAM Account Name as well. This is so I know whether the person even still exists in the company. The below script works perfectly, but...if there is no existing SAM name, it doesn't bother to include the display name. I would like to have the field called SamAccountName just put in some text like "To be removed" if there is no matching AD account. I sure it's a simple conditional check, but my PowerShell game is weak.
Import-Csv c:\temp\DisplayName.csv | ForEach {
Get-ADUser -Filter "DisplayName -eq '$($_.DisplayName)'" -Properties Name, SamAccountName, Company |
Select Name, SamAccountName, Company
} | Export-CSV -path C:\temp\SamAccountName.csv -NoTypeInformation
You can use a calculated property for that. That way you pass everything from the original CSV, and just add in the samaccountname you want.
Import-Csv c:\temp\DisplayName.csv | Select *,#{l='samAccountName';e={Get-ADUser -Filter "DisplayName -eq '$($_.DisplayName)'" -Properties Name, SamAccountName, Company | Select -Expand SamAccountName}} | Export-CSV -path C:\temp\SamAccountName.csv -NoTypeInformation

Finding out if the same property occurs on multiple AD users

I'm pretty new on Powershell and this is by far the trickiest task I have gotten so far. I want to write a script that shows me if the same personal identity number occurs on multiple AD users.
I have managed to get a list of all AD users and their ID numbers using the Powershell Active Directory module and the following:
Get-ADUser -Filter * -SearchBase "OU=X,DC=X,DC=X,DC=X" -Properties PersonalIdentityNumber | Select-Object Name,PersonalIdentityNumber | Where-Object {$_.PersonalIdentityNumber} | Sort-Object -Property PersonalIdentityNumber
Although, I am not sure where to go from there. I suspect that I will have to use a for or foreach loop in some way, but I have tested a bit and not made any concluions. It will most likely be too heavy to compare every user against all other users, but I think that every user can be compared to the 20 users before or after, since matching ID numbers will probably be on users with the same name.
Any ideas on how to accomplish this?
Use the Group-Object cmdlet to group the users based on the value of the PersonalIdentityNumber property:
$usersWithPIN = Get-ADUser -Filter * -SearchBase "OU=X,DC=X,DC=X,DC=X" -Properties PersonalIdentityNumber | Select-Object Name,PersonalIdentityNumber | Where-Object {$_.PersonalIdentityNumber}
$usersWithSamePINGroups = $usersWithPIN |Group-Object PersonalIdentityNumber |Where-Object Count -gt 1
$usersWithSamePINGroups will now contain zero or more Group objects with a Count property (the number of users sharing a given PIN), and a Group property containing the user objects in question

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.

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