Solved

powerwhell : remove the CN=xxxxx only in distinguishedName

Posted on 2016-11-25
3
13 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 82

Expert Comment

by:oBdA
Comment Utility
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
Comment Utility
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 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 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

10 Experts available now in Live!

Get 1:1 Help Now