Solved

powerwhell : remove the CN=xxxxx only in distinguishedName

Posted on 2016-11-25
3
33 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 83

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 83

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
A short film showing how OnPage and Connectwise integration works.

919 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now