Solved

powerwhell : remove the CN=xxxxx only in distinguishedName

Posted on 2016-11-25
3
83 Views
Last Modified: 2016-11-25
Dear All,

I am creating a powershell script that list AD users who did'nt logon within 90 days. the script will export the result in a delimited .txt file

Search-ADAccount -UsersOnly -SearchBase "OU=Europe,OU=Root,DC=contoso,DC=com"-AccountInactive -TimeSpan 90 | Get-ADUser -Properties Name, sAMAccountName, givenName, st, userAccountControl | Where {($_.userAccountControl -band 2) -eq $False} | Select Name, sAMAccountName, givenName, st, distinguishedName | export-csv c:\trash\90day-Inactive-users.txt -NoTypeInformation

I am getting the result in the attached file.

How can I remove CN=Toto Necessite, and only gets OU=Sales Administration Export,OU=Sedentary,OU=Users,OU=London,OU=UK,OU=Europe,OU=Root,DC=contoso,DC=com in the DistinguishedName field?

Is there any way to achieve that?

Thks in advance

JJC
result.PNG
0
Comment
Question by:celmajj
  • 2
3 Comments
 
LVL 84

Expert Comment

by:oBdA
ID: 41901787
You can do that with a "calculated property":
Search-ADAccount -UsersOnly -SearchBase "OU=Europe,OU=Root,DC=contoso,DC=com"-AccountInactive -TimeSpan 90 |
	Get-ADUser -Properties Name, sAMAccountName, givenName, st, userAccountControl |
	Where-Object {($_.userAccountControl -band 2) -eq $False} |
	Select-Object Name, sAMAccountName, givenName, st, @{n='OU'; e={($_.distinguishedName -split ',OU=', 2)[1]}} |
	Export-Csv c:\trash\90day-Inactive-users.txt -NoTypeInformation

Open in new window

0
 

Author Comment

by:celmajj
ID: 41901818
Thks oBdA,

This is what I want. And If I just want to display OU=Sales Administration Export not the others OU, How can I achieve this?.

Thks
JJ
0
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 41901835
Just remove the ", 2" in the -split expression:
Search-ADAccount -UsersOnly -SearchBase "OU=Europe,OU=Root,DC=contoso,DC=com"-AccountInactive -TimeSpan 90 |
	Get-ADUser -Properties Name, sAMAccountName, givenName, st, userAccountControl |
	Where-Object {($_.userAccountControl -band 2) -eq $False} |
	Select-Object Name, sAMAccountName, givenName, st, @{n='OU'; e={($_.distinguishedName -split ',OU=')[1]}} |
	Export-Csv c:\trash\90day-Inactive-users.txt -NoTypeInformation

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question