Solved

powerwhell : remove the CN=xxxxx only in distinguishedName

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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