PowerShell Management Series (33) PowerShell operations Query the OU storage location for AD accounts

Source: Internet
Author: User
Tags samaccountname

-----provide ad\exchange\lync\sharepoint\crm\sc\o365 and other Microsoft product implementation and outsourcing, qq:185426445. Phone 18666943750


Requirements: Find out where each ad user's OU is located

Step 1:2008r2 The AD domain environment, the command is as follows

Import-module ActiveDirectory

$user =get-aduser-filter *-properties *-searchbase "dc=xxx,dc=com" |select-expandproperty sAMAccountName

foreach ($i in $user) '

{

$a =get-aduser $i-properties * |% {$_. CanonicalName}

$b = $a. Split ("/")

Get-aduser $i-properties * |select samaccountname,@{n= "OU"; e={$b [$b. count-2]}},canonicalname

}

Or:

Import-module ActiveDirectory

$user =get-aduser-filter *-properties *-searchbase "dc=xxx,dc=com" |select-expandproperty sAMAccountName

foreach ($i in $user) '

{

$a =get-aduser $i-properties * |% {$_. CanonicalName}

$b = $a. Split ("/")

Get-aduser $i-properties * |select samaccountname,@{n= "OU"; e={$b [ -2]}},canonicalname

}

Such as:

650) this.width=650; "Width=" "height=" 251 "title=" 1.png "style=" WIDTH:704PX;HEIGHT:324PX; "alt=" Wkiol1f3crbhxeyhaaedjrc50z0853.png-wh_50 "src=" http://s5.51cto.com/wyfs02/M02/88/6C/ Wkiol1f3crbhxeyhaaedjrc50z0853.png-wh_500x0-wm_3-wmp_4-s_3175465030.png "/>


Step 2, 2012/2012R2 the AD domain environment, the command is as follows

Get-aduser-filter *-properties *-searchbase "DC=XXX,DC=COM,DC=CN" |select samaccountname,@{n= "path"; e={$a =$_. Canonicalname-split "/"; $a [ -2]}},canonicalname

Such as:

650) this.width=650; "Width=" 501 "height=" 167 "title=" 2.png "style=" WIDTH:701PX;HEIGHT:256PX; "alt=" Wkiom1f3cshwid5kaahtib4rgnc542.png-wh_50 "src=" http://s1.51cto.com/wyfs02/M00/88/70/ Wkiom1f3cshwid5kaahtib4rgnc542.png-wh_500x0-wm_3-wmp_4-s_4237141062.png "/>


Step 3, find out the specific path of each ad account, the command is as follows

$user =get-aduser-filter *-properties *-searchbase "DC=XXX,DC=COM,DC=CN" |select-expandproperty sAMAccountName

foreach ($i in $user) '

{ `

$a =get-aduser $i-properties * |% {$_. distinguishedname}

$b = $a-split ","

$c = ""

for ($j =1; $j-le $b. Count-2; $j + +) {$c + = $b [$j]+ ","}

$c = $c + $b [-1]

Get-aduser $i-properties * |select samaccountname,@{n= "Path"; e={$c}},distinguishedname

}

650) this.width=650; "Width=" 501 "height=" 322 "title=" QQ picture 20161008002001.png "style=" WIDTH:696PX;HEIGHT:383PX; "alt= "Wkiol1f3y0kar5pcaaehq7gwvrm315.png-wh_50" src= "http://s5.51cto.com/wyfs02/M02/88/6E/ Wkiol1f3y0kar5pcaaehq7gwvrm315.png-wh_500x0-wm_3-wmp_4-s_483886275.png "/>


This article is from the "Zhou Ping Microsoft Technology Exchange Platform" blog, please be sure to keep this source http://yuntcloud.blog.51cto.com/1173839/1859125

PowerShell Management Series (33) PowerShell operations Query the OU where the ad account corresponds

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.