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

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

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

Select-Object -Property returning empty values for certain most properties Powershell

I'm using the below line of code to get a list of computers from Active Directory and then export them to a CSV file along with certain properties. However, only "SamAccountName" and "DNSHostName" are returning values inside the CSV file. All other properties are empty.
Get-ADComputer -filter * | Select-Object -Property SamAccountName,DNSHostName,PasswordLastSet,whenCreated,accountExpirationDate,operatingSystem,operatingSystemServicePack,operatingSystemVersion,userAccountControl,LastLogonDate |export-csv -path "C:\Users\output.csv"
When I choose only the property "PasswordLastSet", the CSV file looks odd and returns only true and false as opposed to a date and time. For instance, here are the first three lines:
#TYPE Selected.Microsoft.ActiveDirectory.Management.ADComputer
enabled
TRUE
Is there something wrong with the way I'm approaching this?
AD objects like ADComputer have a lot of properties and Get-ADComputer, by default, retrieves just a few of those properties for performance reasons. Use the -properties parameter to specify the properties you need.
Get-ADComputer -filter * -Properties SamAccountName,DNSHostName,PasswordLastSet,whenCreated,accountExpirationDate,operatingSystem,operatingSystemServicePack,operatingSystemVersion,userAccountControl,LastLogonDate
| Select-Object -Property SamAccountName,DNSHostName,PasswordLastSet,whenCreated,accountExpirationDate,operatingSystem,operatingSystemServicePack,operatingSystemVersion,userAccountControl,LastLogonDate
| export-csv -path "C:\Users\output.csv"
Use -properties * to get all

Powershell to tally up departments in a file

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

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 pipe variables into get-user command

I am trying to pipe a list of email addresses into a get-user command in powershell
$email = get-content -path "c:\temp\file.csv" get-user -indentity $email | select-object userprincipalname,department,phone,name | format-table | out-file c:\temp\file.txt
Welcome to SO.
Lets see... where to start
Email Address is not one of the value recognized for Identity
It is spelled -identity
Don't use Format-table for object output.
Department is not one of the default values returned.
There is no AD attribute just called phone
It's Get-Aduser not get-user
Don't know if it was just a copy paste accident but you have multiple lines as one.
-Identity expects one value. Not an array of names.
Knowing that lets see if we can take a stab at what you were trying to do. Assuming that your file "c:\temp\file.csv" only contained addresses with no header (since that is how you were treating it.)
Get-Content c:\temp\file.csv | ForEach-Object{
Get-ADUser -Filter "emailaddress -eq '$_'" -Properties department,OfficePhone
} | Select-Object UserPrincipalName,Department,OfficePhone,Name | Export-CSV C:\temp\outputfile.csv -NoTypeInformation
There is no error correction here so you might need to look into an -ErrorAction try/catch combination. I encourage you to look that up on your own.