Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

powerwhell : remove the CN=xxxxx only in distinguishedName

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Set OWA language and time zone in Exchange for individuals, all users or per database.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

861 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