<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>powershell get ad user Archives - the Sysadmin Channel</title>
	<atom:link href="https://thesysadminchannel.com/tag/powershell-get-ad-user/feed/" rel="self" type="application/rss+xml" />
	<link>https://thesysadminchannel.com/tag/powershell-get-ad-user/</link>
	<description>Documenting My Life as a System Administrator</description>
	<lastBuildDate>Mon, 01 Nov 2021 21:33:14 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.2</generator>
<site xmlns="com-wordpress:feed-additions:1">144174110</site>	<item>
		<title>Get-ADUser: Find AD Users Using PowerShell Ultimate Deep Dive</title>
		<link>https://thesysadminchannel.com/get-aduser-find-active-directory-users-using-powershell-ultimate-deep-dive/</link>
					<comments>https://thesysadminchannel.com/get-aduser-find-active-directory-users-using-powershell-ultimate-deep-dive/#respond</comments>
		
		<dc:creator><![CDATA[Paul Contreras]]></dc:creator>
		<pubDate>Sat, 30 Oct 2021 01:54:18 +0000</pubDate>
				<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[get ad user properties]]></category>
		<category><![CDATA[get aduser filter]]></category>
		<category><![CDATA[get-aduser]]></category>
		<category><![CDATA[powershell get ad user]]></category>
		<category><![CDATA[powershell get ad user attributes]]></category>
		<guid isPermaLink="false">https://thesysadminchannel.com/?p=3508</guid>

					<description><![CDATA[<p>Get-ADUser, Arguably one of the most used cmdlets I use on a day to day basis. I&#8217;m sure the same goes for other sysadmins around the world if they&#8217;re managing a Windows environment. Today we&#8217;re going to do a deep&#8230; <a href="https://thesysadminchannel.com/get-aduser-find-active-directory-users-using-powershell-ultimate-deep-dive/" class="more-link">Continue Reading <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a href="https://thesysadminchannel.com/get-aduser-find-active-directory-users-using-powershell-ultimate-deep-dive/">Get-ADUser: Find AD Users Using PowerShell Ultimate Deep Dive</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><strong>Get-ADUser</strong>, Arguably one of the most used cmdlets I use on a day to day basis.  I&#8217;m sure the same goes for other sysadmins around the world if they&#8217;re managing a Windows environment.  Today we&#8217;re going to do a deep dive on Get-ADUser and the multiple ways to find Active Directory users using Powershell.  As always, let&#8217;s touch on the requirements needed to use Get-ADUser.<br />
&nbsp;</p>
<div id="tableofcontents">
<h2>Table Of Contents</h2>
<ul>
<li><a href="#Requirements">Requirements</a></li>
<li><a href="#Examples">Get-ADUser Examples and Parameter Overview</a></li>
<ul>
<li><a href="#IdentityParameter">Find AD User With Identity Parameter</a></li>
<li><a href="#FilterParameter">Get AD User Using The Filter Parameter</a></li>
<ul>
<li><a href="#FilterByProperty">Filter By Property</a></li>
<li><a href="#FilterByOperator">Filter by Operator</a></li>
<li><a href="#FilterWithLDAP">How To Use LDAP Filters</a></li>
<li><a href="#FilterANR">Filter Using Ambiguous Name Resolution (ANR)</a></li>
</ul>
<li><a href="#PropertyParameter">Display All Of The Properties For A Specified User</a></li>
<li><a href="#SearchBaseParameter">Query Active Directory Users By Organizational Unit</a></li>
<ul>
<li><a href="#SearchScopeParameter">Specify The OU Depth Of A Search</a></li>
</ul>
<li><a href="#ServerParameter">Target The Domain Controller Of Your Choice</a></li>
<li><a href="#CredentialParameter">Passing Alternate Credentials</a></li>
</ul>
<li><a href="#QueryAlternateDomain">Get-ADUser From A Different Domain</a></li>
<li><a href="#Conclusion">Conclusion</a></li>
</ul>
</div>
<div id="Requirements" style="scroll-margin-top: 15px;"></div>
<h2>Requirements</h2>
<p>Using the Active Directory Module has a few requirements that we&#8217;ll need to make sure are up and running in order for your queries to run successfully.</p>
<ul>
<li>An Active Directory Domain must be setup</li>
<li>The Domain Controller you&#8217;re querying must have Active Directory Web Services Service running</li>
<li>Remote Server Administration Tools (RSAT)</li>
<ul>
<li>For Windows 10 1903 and later, <a href="https://thesysadminchannel.com/install-rsat-for-windows-10-version-1809-1903-and-later/" rel="noopener" target="_blank">view setup guide</a></li>
<li>Active Directory Light-Weight Directory Tools Windows Feature (RSAT-AD-Tools) if running on a Windows Server</li>
</ul>
</ul>
<div id="Examples" style="scroll-margin-top: 15px;"></div>
<h2>Get-ADUser Examples and Parameter Overview</h2>
<p>In this article we&#8217;ll cover several of the parameters used in the cmdlet along with examples and screenshots so you can see exactly how to utilize these to your benefit. </p>
<div id="IdentityParameter" style="scroll-margin-top: 15px;"></div>
<h2>Find ADUser With Identity Parameter</h2>
<p>Get-ADUser using the -Identity Parameter is typically the most commonly used parameter when people want to query a specific user.  This is because the -Identity parameter is positioned as the first parameter so it can be omitted when running the actual query.</p>
<p> Example: <code>Get-ADUser -Identity aryastark</code> will produce the exact same results as <code>Get-ADUser aryastark</code><br />
<a href="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Identity-Parameter.png" target="_blank" rel="noopener"><img fetchpriority="high" decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Identity-Parameter.png" alt="Get AD User -Identity Parameter" width="1099" height="632" class="aligncenter size-full wp-image-3547" srcset="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Identity-Parameter.png?v=1635202532 1099w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Identity-Parameter-1024x589.png?v=1635202532 1024w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Identity-Parameter-768x442.png?v=1635202532 768w" sizes="(max-width: 1099px) 100vw, 1099px" /></a></p>
<p>&nbsp;<br />
There are 4 attributes that are allowed when using Identity parameter. Let&#8217;s list them here along with an example of what it typically looks like.</p>
<ul>
<li>Distinguished Name</li>
<ul>
<li>CN=Arya Stark,OU=Excluded,DC=ad,DC=thesysadminchannel,DC=com</li>
</ul>
</ul>
<ul>
<li>ObjectGuid</li>
<ul>
<li>643d7cb4-9682-4835-908d-d696ed476649</li>
</ul>
</ul>
<ul>
<li>Security Identifier (SID)</li>
<ul>
<li>S-1-5-21-3946430794-117524452-1540306727-8620</li>
</ul>
</ul>
<ul>
<li>sAMAccountName (username)</li>
<ul>
<li>aryastark</li>
</ul>
</ul>
<div id="attachment_3549" style="width: 888px" class="wp-caption aligncenter"><a href="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-AD-User-Powershell-Identity-Parameter.png" target="_blank" rel="noopener"><img decoding="async" aria-describedby="caption-attachment-3549" src="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-AD-User-Powershell-Identity-Parameter.png" alt="Get AD User Powershell Identity Parameter" width="878" height="277" class="size-full wp-image-3549" srcset="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-AD-User-Powershell-Identity-Parameter.png?v=1635203039 878w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-AD-User-Powershell-Identity-Parameter-768x242.png?v=1635203039 768w" sizes="(max-width: 878px) 100vw, 878px" /></a><p id="caption-attachment-3549" class="wp-caption-text">Example of the 4 attributes that are accepted</p></div>
<div id="FilterParameter" style="scroll-margin-top: 15px;"></div>
<h2>Get-ADUser Using The Filter Parameter</h2>
<p>The -Filter parameter in the Get-ADUser cmdlet is definitely also another fan favorite.  The phrase &#8220;Filter Left, Format right&#8221;  definitely applies here in getting the data you need in a reduced amount of time.  This is one of those fundamental Powershell concepts that everyone should learn early on.</p>
<p>&nbsp;</p>
<div id="blockquote1">
<strong>Pro-Top</strong>: Replace Where-Object with Filter.  Anytime a filter parameter is available use that instead of Where-Object for faster results.
</div>
<p>&nbsp;</p>
<div id="FilterByProperty" style="scroll-margin-top: 15px;"></div>
<h3>Get AD User Properties</h3>
<p>Let&#8217;s take a look at get ad user properties in action.  Say we wanted to get everyone with the GivenName (firstname) of &#8216;Arya&#8217; &#8211; What exactly would that query look like?</p>
<pre class="brush: powershell; title: ; notranslate">
#Get All Active Directory users that have a first name of Arya
Get-ADUser -Filter &quot;GivenName -eq 'Arya'&quot; | select Name, UserPrincipalName, Enabled

Name       UserPrincipalName                Enabled
----       -----------------                -------
Arya Stark aryastark@thesysadminchannel.com    True
Arya Cruz  aryacruz@thesysadminchannel.com     True
Arya Jolie aryajolie@thesysadminchannel.com    True
</pre>
<div id="attachment_3591" style="width: 928px" class="wp-caption aligncenter"><a href="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-Parameter-GivenName.png" target="_blank" rel="noopener"><img decoding="async" aria-describedby="caption-attachment-3591" src="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-Parameter-GivenName.png" alt="Get-ADUser -Filter Parameter GivenName" width="918" height="258" class="size-full wp-image-3591" srcset="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-Parameter-GivenName.png?v=1635320307 918w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-Parameter-GivenName-768x216.png?v=1635320307 768w" sizes="(max-width: 918px) 100vw, 918px" /></a><p id="caption-attachment-3591" class="wp-caption-text">Select object was added to condense output</p></div>
<p>You can find other filterable attributes by choosing any one of the attributes when running <code>-Properties *</code>.  Commonly used filters are <em>UserPrincipalName</em>, <em>Surname</em>, <em>Mail</em> and even <em>Name</em> or <em>DisplayName</em>.  </p>
<div id="FilterByOperator" style="scroll-margin-top: 15px;"></div>
<h3>Filter With Operators</h3>
<p>Regarding operators, there are several choices such as equal, like, less than and even greater than that&#8217;s convenient for us to use.  </p>
<p>When using the <code>-eq</code> operator, the filter has to match the property exactly so make sure you specify the text exactly as it&#8217;s shown in AD.  As noted in the above example, we searched for all users with the first name &#8216;Arya.&#8217;  Say we wanted to only filter for the Name &#8216;Arya Stark&#8217;.  </p>
<pre class="brush: powershell; title: ; notranslate">
#Get the AD user whos name is Arya Stark
Get-ADUser -Filter &quot;Name -eq 'Arya Stark'&quot; | select Name, UserPrincipalName, Enabled


Name       UserPrincipalName                Enabled
----       -----------------                -------
Arya Stark aryastark@thesysadminchannel.com    True
</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-Name-eq-Arya.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-Name-eq-Arya.png" alt="Get-ADUser -Filter Name eq Arya" width="875" height="231" class="aligncenter size-full wp-image-3607" srcset="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-Name-eq-Arya.png?v=1635381251 875w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-Name-eq-Arya-768x203.png?v=1635381251 768w" sizes="(max-width: 875px) 100vw, 875px" /></a><br />
&nbsp;</p>
<p>Let&#8217;s now dive into the <code>-like</code> operator and how to specifically use it for filters.  A great example I&#8217;ve used in the past is to see who are all the people that have the word Remote in their AD Office Attribute.</p>
<pre class="brush: powershell; title: ; notranslate">
#Get all users who are remote 
Get-ADUser -Filter &quot;Office -like 'Remote*'&quot; -Properties Office | select UserPrincipalName, Name, Office

UserPrincipalName                Name       Office
-----------------                ----       ------
aryacruz@thesysadminchannel.com  Arya Cruz  Remote - California
aryastark@thesysadminchannel.com Arya Stark Remote - Winterfell


#Get all users who are in California
Get-ADUser -Filter &quot;Office -like '*California*'&quot; -Properties Office | select UserPrincipalName, Name, Office

UserPrincipalName                Name       Office
-----------------                ----       ------
aryacruz@thesysadminchannel.com  Arya Cruz  Remote - California
aryajolie@thesysadminchannel.com Arya Jolie Palo Alto - California
</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-Office-like-Operator.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-Office-like-Operator.png" alt="Get-ADUser -Filter Office like Operator" width="1099" height="386" class="aligncenter size-full wp-image-3611" srcset="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-Office-like-Operator.png?v=1635382216 1099w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-Office-like-Operator-1024x360.png?v=1635382216 1024w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-Office-like-Operator-768x270.png?v=1635382216 768w" sizes="(max-width: 1099px) 100vw, 1099px" /></a><br />
&nbsp;</p>
<p>With regard to auditing, I&#8217;ve always found <a href="https://thesysadminchannel.com/get-last-logon-date-for-all-users-in-your-domain/" rel="noopener" target="_blank">filtering accounts by LastLogonDate</a> has always been extremely helpful. For an in-depth write-up check out the link above.  Otherwise, let&#8217;s go over a quick example to get the gist of what&#8217;s happening.  We&#8217;ll also couple it with the <code>-and</code> operator to string multiple queries together and narrow down your filter.</p>
<pre class="brush: powershell; title: ; notranslate">
#Get Remote Users who have not logged in, in over 90 days
$CutoffDate = (Get-Date).AddDays(-90)
Get-ADUser -Filter &quot;LastLogonDate -lt '$CutoffDate' -and Office -like '*Remote*'&quot; -Properties LastLogonDate `
 | select UserPrincipalName, Name, LastLogonDate

UserPrincipalName                Name       LastLogonDate
-----------------                ----       -------------
aryastark@thesysadminchannel.com Arya Stark 3/17/2021 5:29:46 PM
</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-LastLogonDate.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-LastLogonDate.png" alt="Get-ADUser -Filter LastLogonDate" width="1099" height="250" class="aligncenter size-full wp-image-3613" srcset="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-LastLogonDate.png?v=1635383598 1099w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-LastLogonDate-1024x233.png?v=1635383598 1024w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-LastLogonDate-768x175.png?v=1635383598 768w" sizes="(max-width: 1099px) 100vw, 1099px" /></a></p>
<p>&nbsp;</p>
<div id="FilterWithLDAP" style="scroll-margin-top: 15px;"></div>
<h3>How To Use LDAP Filters</h3>
<p>To be perfectly honest, I can probably count the number of times on one hand that I&#8217;ve used an LDAP filter.  The methods mentioned above have been ingrained into my brain since that&#8217;s how I learned.  The reason being is that the syntax is a bit more complex and the standard operators like -and/-or don&#8217;t really come into play here.  </p>
<p>If you&#8217;re great with VBScript then it might be up your alley.  In any event, here we go.</p>
<pre class="brush: powershell; title: ; notranslate">
#Get AD user using an LDAP filter query
Get-ADUser -LdapFilter &quot;(&amp;(objectClass=user)(Name=Arya Stark))&quot; | select Name, UserPrincipalName, Enabled

Name       UserPrincipalName                Enabled
----       -----------------                -------
Arya Stark aryastark@thesysadminchannel.com    True
</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-LDAPFilter.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-LDAPFilter.png" alt="Get Active Directory User -LDAPFilter" width="1028" height="217" class="aligncenter size-full wp-image-3623" srcset="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-LDAPFilter.png?v=1635542661 1028w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-LDAPFilter-1024x216.png?v=1635542661 1024w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-LDAPFilter-768x162.png?v=1635542661 768w" sizes="(max-width: 1028px) 100vw, 1028px" /></a></p>
<p>&nbsp;</p>
<div id="FilterANR" style="scroll-margin-top: 15px;"></div>
<h3>Filter Using Ambiguous Name Resolution (ANR)</h3>
<p>Ambiguous Name Resolution, aka ANR, allows multiple objects to be resolved on a single query. Think of it like a built-in -like operator that queries against GivenName, Surname, DisplayName, SamAccountName, physicalDeliveryOfficeName and even the Exchange MailNickName without any added effort.  </p>
<p>ANR is especially useful in larger organizations where people share a similar display name.  It just helps to truncate multiple -and/-or queries into a single function to ease your searches. Let&#8217;s cover an example of using ambiguous name resolution in an actual filter (using Arya Stark as our example).</p>
<pre class="brush: powershell; title: ; notranslate">
#Get all users who have Arya in their name
Get-ADUser -Filter &quot;Anr -eq 'Arya'&quot; | select UserPrincipalName, Name, Enabled

UserPrincipalName                Name       Enabled
-----------------                ----       -------
aryastark@thesysadminchannel.com Arya Stark    True
aryacruz@thesysadminchannel.com  Arya Cruz     True
aryajolie@thesysadminchannel.com Arya Jolie    True


#Get all users who have Stark in their name
Get-ADUser -Filter &quot;Anr -eq 'Stark'&quot; | select UserPrincipalName, Name, Enabled

UserPrincipalName                Name       Enabled
-----------------                ----       -------
aryastark@thesysadminchannel.com Arya Stark    True

</pre>
<div id="attachment_3629" style="width: 859px" class="wp-caption aligncenter"><a href="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-ANR.png" target="_blank" rel="noopener"><img decoding="async" aria-describedby="caption-attachment-3629" src="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-ANR.png" alt="Get-ADUser -Filter ANR" width="849" height="397" class="size-full wp-image-3629" srcset="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-ANR.png?v=1635546852 849w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-ANR-768x359.png?v=1635546852 768w" sizes="(max-width: 849px) 100vw, 849px" /></a><p id="caption-attachment-3629" class="wp-caption-text">Notice we didn&#8217;t need to specify GivenName, Surname or even use the -Like Operator.</p></div>
<p>&nbsp;</p>
<div id="PropertyParameter" style="scroll-margin-top: 15px;"></div>
<h2>Display All Of The Properties For A Specified User</h2>
<p>All Active Directory users have the same core attributes populated but they&#8217;re not displayed by default.  If you notice in the examples above, I had to specify <code>-Property</code> in order for Powershell to know to check those AD properties.  If you omit the property parameter, the filter won&#8217;t find it even though the attribute is there on the user&#8217;s account.</p>
<p>A good thing is this allows a wildcard (*) so you can see what&#8217;s available.  I would also recommend to explicitly specify your properties when querying many users so you&#8217;re not putting to much stress on the remote Domain Controller. </p>
<pre class="brush: powershell; title: ; notranslate">
#Get all properties for a user.
Get-ADUser aryastark -Properties * 

</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Property-All.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Property-All.png" alt="Get Active Directory User -Property All" width="1099" height="632" class="aligncenter size-full wp-image-3626" srcset="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Property-All.png?v=1635544819 1099w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Property-All-1024x589.png?v=1635544819 1024w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Property-All-768x442.png?v=1635544819 768w" sizes="(max-width: 1099px) 100vw, 1099px" /></a></p>
<div id="SearchBaseParameter" style="scroll-margin-top: 15px;"></div>
<h2>Query Active Directory Users By Organizational Unit</h2>
<p>The ability to query users by an Organizational Unit is an excellent method to ensure you&#8217;re getting the most out of your Active Directory OU structure.  A great, real world example for this would be if you have your AD Org units structured by regional location and you&#8217;re looking to get all users in that location.</p>
<p>SearchBase uses the DistinguishedName as the parameter input.  You can grab the DN by one of 2 ways.  </p>
<ul>
<li>Query a user in that OU and select the DN property. Extract OU DN from there</li>
<li>Use Get-ADOrganizationalUnit and filter by name</li>
</ul>
<pre class="brush: powershell; title: ; notranslate">
#Query a user in the OU and select the DN property to get the OU syntax.
Get-ADUser aryastark 

DistinguishedName
-----------------
CN=Arya Stark,OU=Excluded,DC=ad,DC=thesysadminchannel,DC=com


#Use Get-ADOrganizationalUnit and filter by name
Get-ADOrganizationalUnit -Filter &quot;Name -like '*Excluded*'&quot; | select DistinguishedName

DistinguishedName
-----------------
OU=Excluded,DC=ad,DC=thesysadminchannel,DC=com
</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-SearchBase.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-SearchBase.png" alt="Get Active Directory User -SearchBase" width="872" height="346" class="aligncenter size-full wp-image-3633" srcset="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-SearchBase.png?v=1635549335 872w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-SearchBase-768x305.png?v=1635549335 768w" sizes="(max-width: 872px) 100vw, 872px" /></a></p>
<p>&nbsp;</p>
<p>Now that we have the Organizational Unit&#8217;s DistinguishedName, we can use that as the input parameter.  This coupled with the -Filter parameter will help narrow your search by Org Unit.</p>
<pre class="brush: powershell; title: ; notranslate">
#Get All users under the Excluded OU.  Use a custom label to show Organizational Unit
Get-ADUser -Filter * -SearchBase &quot;OU=Excluded,DC=ad,DC=thesysadminchannel,DC=com&quot; | select Name, `
@{Name = 'OrganizationalUnit'; `
Expression = {$Length = ($_.DistinguishedName).IndexOf(&quot;,OU&quot;); $_.DistinguishedName.Substring($Length + 1) }} | `
Sort-Object OrganizationalUnit

Name               OrganizationalUnit
----               ------------------
Arya Jolie         OU=Excluded,DC=ad,DC=thesysadminchannel,DC=com
Isabella Contreras OU=Excluded,DC=ad,DC=thesysadminchannel,DC=com
Arya Cruz          OU=Excluded,DC=ad,DC=thesysadminchannel,DC=com
Director of IT     OU=Excluded,DC=ad,DC=thesysadminchannel,DC=com
Arya Stark         OU=Excluded,DC=ad,DC=thesysadminchannel,DC=com
Test1              OU=Test,OU=Excluded,DC=ad,DC=thesysadminchannel,DC=com
Test2              OU=Test,OU=Excluded,DC=ad,DC=thesysadminchannel,DC=com

</pre>
<div id="attachment_3636" style="width: 1074px" class="wp-caption aligncenter"><a href="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-All-SearchBase-OU.png" target="_blank" rel="noopener"><img decoding="async" aria-describedby="caption-attachment-3636" src="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-All-SearchBase-OU.png" alt="Get-ADUser Filter All -SearchBase OU" width="1064" height="390" class="size-full wp-image-3636" srcset="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-All-SearchBase-OU.png?v=1635552095 1064w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-All-SearchBase-OU-1024x375.png?v=1635552095 1024w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-All-SearchBase-OU-768x282.png?v=1635552095 768w" sizes="(max-width: 1064px) 100vw, 1064px" /></a><p id="caption-attachment-3636" class="wp-caption-text">Wildcards are also allowed to use with Filter to search for All</p></div>
<div id="SearchScopeParameter" style="scroll-margin-top: 15px;"></div>
<h3>Specify The OU Depth Of A Search</h3>
<p>Building off of the SearchBase parameter from above, you might have noticed that the search was recursive.  Meaning that it drilled down to all Sub OU&#8217;s without having the need to specify them.  The question however, is what if we don&#8217;t want to drill down.  What if we only want <em>that</em> explicit OU? </p>
<p>This is where the SearchScope parameter comes into play. Using the same query above, let&#8217;s exclude the two test accounts in the Test OU.</p>
<pre class="brush: powershell; title: ; notranslate">
#Get Users in the Excluded OU and Exclude the Test OU Users
Get-ADUser -Filter * -SearchBase &quot;OU=Excluded,DC=ad,DC=thesysadminchannel,DC=com&quot; -SearchScope OneLevel | `
select Name, `
@{Name = 'OrganizationalUnit'; `
Expression = {$Length = ($_.DistinguishedName).IndexOf(&quot;,OU&quot;); $_.DistinguishedName.Substring($Length + 1) }}

Name               OrganizationalUnit
----               ------------------
Arya Cruz          OU=Excluded,DC=ad,DC=thesysadminchannel,DC=com
Arya Jolie         OU=Excluded,DC=ad,DC=thesysadminchannel,DC=com
Arya Stark         OU=Excluded,DC=ad,DC=thesysadminchannel,DC=com
Director of IT     OU=Excluded,DC=ad,DC=thesysadminchannel,DC=com
Isabella Contreras OU=Excluded,DC=ad,DC=thesysadminchannel,DC=com

</pre>
<div id="attachment_3639" style="width: 1062px" class="wp-caption aligncenter"><a href="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-All-SearchBase-SearchScope-OU.png" target="_blank" rel="noopener"><img decoding="async" aria-describedby="caption-attachment-3639" src="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-All-SearchBase-SearchScope-OU.png" alt="Get-ADUser Filter All -SearchBase SearchScope OU" width="1052" height="359" class="size-full wp-image-3639" srcset="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-All-SearchBase-SearchScope-OU.png?v=1635552975 1052w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-All-SearchBase-SearchScope-OU-1024x349.png?v=1635552975 1024w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Filter-All-SearchBase-SearchScope-OU-768x262.png?v=1635552975 768w" sizes="(max-width: 1052px) 100vw, 1052px" /></a><p id="caption-attachment-3639" class="wp-caption-text">When SearchScope is omitted, it will default to Subtree</p></div>
<p>&nbsp;</p>
<div id="ServerParameter" style="scroll-margin-top: 15px;"></div>
<h2>Target The Domain Controller Of Your Choice</h2>
<p>Anytime you make an Active Directory query, you&#8217;ll most likely always default to a Domain Controller in your site.  This is defined by Active Directory Sites and Services and an easy way to check what Domain Controller you&#8217;re currently authenticating against is to use <code>$env:LogonServer</code>.</p>
<p>&nbsp;</p>
<p>This is great and all, but what if you wanted to query a Domain Controller in another site, perhaps one across the globe?  You would use the <code>-Server</code> parameter to do this.  Specifically for me, I always like to use the Primary Domain Controller, PDC Emulator, as this is the heart of all replication changes.  If you specify this Domain Controller specifically, you can avoid waiting for replication and can move on with your script without adding sleep commands. </p>
<p>You can use Powershell to query, or <a href="https://thesysadminchannel.com/how-to-transfer-fsmo-roles-in-server-2019-using-powershell/" rel="noopener" target="_blank">transfer FSMO Roles to a different Domain Controller</a>.</p>
<p>Let&#8217;s walk through an example for how to use the server parameter to specify the PDC emulator dynamically.</p>
<pre class="brush: powershell; title: ; notranslate">
#Get PDC Emulator dynamically and save it to a variable for later use
$DomainController = Get-ADDomain | select -ExpandProperty PDCEmulator
$DomainController
PAC-DC01.ad.thesysadminchannel.com

Get-ADUser aryastark -Server $DomainController | select UserPrincipalName, Name

UserPrincipalName                Name
-----------------                ----
aryastark@thesysadminchannel.com Arya Stark

</pre>
<div id="attachment_3641" style="width: 859px" class="wp-caption aligncenter"><a href="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Server-Parameter.png" target="_blank" rel="noopener"><img decoding="async" aria-describedby="caption-attachment-3641" src="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Server-Parameter.png" alt="Get-ADUser Server Parameter" width="849" height="314" class="size-full wp-image-3641" srcset="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Server-Parameter.png?v=1635554260 849w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Server-Parameter-768x284.png?v=1635554260 768w" sizes="(max-width: 849px) 100vw, 849px" /></a><p id="caption-attachment-3641" class="wp-caption-text">Using the Server parameter can bypass replication times and it recommended for automation.</p></div>
<p>&nbsp;</p>
<div id="CredentialParameter" style="scroll-margin-top: 15px;"></div>
<h2>Passing Alternate Credentials for Get-ADUser</h2>
<p>Being able to pass a different set of credentials would come in handy for use cases like automation or other use cases like users in a different domain.  Since Active Directory grants read-only access to all users by default, there really isn&#8217;t a need to pass in alternate credentials if you&#8217;re querying something in the same domain.  It should be able to do it with no problem.</p>
<p>&nbsp;</p>
<p>When this comes in handy is if you need to make changes to AD Objects and you need to use different credentials. To make this happen you&#8217;ll use the <code>-Credential</code> parameter and use <code>Get-Credential</code> to securely set the username and password.  Since we&#8217;re so keen on examples, let&#8217;s test it.</p>
<pre class="brush: powershell; title: ; notranslate">
#Save user credentials into a variable using Get-Credential
$Credential = Get-Credential -UserName 'ad\pcontreras' -Message 'Enter in a Password'
PS C:\&gt;
Get-ADUser aryastark -Credential $Credential | select UserPrincipalName, Name

UserPrincipalName                Name
-----------------                ----
aryastark@thesysadminchannel.com Arya Stark

</pre>
<div id="attachment_3644" style="width: 866px" class="wp-caption aligncenter"><a href="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Credential-Parameter.png" target="_blank" rel="noopener"><img decoding="async" aria-describedby="caption-attachment-3644" src="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Credential-Parameter.png" alt="Get AD User Credential Parameter" width="856" height="278" class="size-full wp-image-3644" srcset="https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Credential-Parameter.png?v=1635555117 856w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Get-ADUser-Credential-Parameter-768x249.png?v=1635555117 768w" sizes="(max-width: 856px) 100vw, 856px" /></a><p id="caption-attachment-3644" class="wp-caption-text">In the sprit of this article, we&#8217;ll pass on credentials for Get-ADUser</p></div>
<p>&nbsp;</p>
<div id="QueryAlternateDomain" style="scroll-margin-top: 15px;"></div>
<h2>Get-ADUser From A Different Domain</h2>
<p>If you happen to have multiple Domains in your forest and you&#8217;re too lazy to Remote Desktop into a Domain Controller on that domain to run the query (guilty of it myself from time to time), it&#8217;s absolutely helpful to be able to run your query from a single machine.  You can do this by combining two of the parameters above.  Those parameters being <code>-Credential</code> as well as <code>-Server</code>.</p>
<p>I don&#8217;t have any other domains in my forest so I won&#8217;t be able to provide a working screenshot. However, one thing to keep in mind is that you&#8217;ll need to provide the Fully Qualified Domain Name (FQDN) for the remote DC.  Overall, the basic syntax should look like this:</p>
<pre class="brush: powershell; title: ; notranslate">
#Save user credentials into a variable using Get-Credential
$Credential = Get-Credential -UserName 'otherdomain\myaccount' -Message 'Enter in a Password'

Get-ADUser myaccount -Server DC01.otherdomain.thesysadminchannel.com -Credential $Credential
</pre>
<p>&nbsp;</p>
<div id="Conclusion" style="scroll-margin-top: 15px;"></div>
<h2>Conclusion</h2>
<p>Hopefully this deep dive on how to use Powershell Get AD User has been incredible helpful for you.  I&#8217;m also hoping you learned a thing or two that you can implement in your environment.  As I mentioned, Get-ADUser is probably one of the most fundamental cmdlets that anyone administrator should have in their arsenal of tools.  </p>
<p>&nbsp;</p>
<p>It can be useful, especially when providing reports on the current state of your environment.  If you liked this article, feel free to browse our other <a href="https://thesysadminchannel.com/active-directory/" rel="noopener" target="_blank">Active Directory</a> as well as our own personal <a href="https://thesysadminchannel.com/powershell/" rel="noopener" target="_blank">Powershell gallery</a> full of useful scripts.  Finally, if you&#8217;re interested in video content, check out our <a href="https://www.youtube.com/c/theSysadminChannel" rel="noopener" target="_blank">Youtube Channel</a> for sysadmin videos</p>
<p>The post <a href="https://thesysadminchannel.com/get-aduser-find-active-directory-users-using-powershell-ultimate-deep-dive/">Get-ADUser: Find AD Users Using PowerShell Ultimate Deep Dive</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://thesysadminchannel.com/get-aduser-find-active-directory-users-using-powershell-ultimate-deep-dive/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3508</post-id>	</item>
	</channel>
</rss>
