{"id":16831,"date":"2010-10-13T00:01:00","date_gmt":"2010-10-13T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/10\/13\/use-powershell-to-search-active-directory-users-missing-values-and-add-same\/"},"modified":"2010-10-13T00:01:00","modified_gmt":"2010-10-13T00:01:00","slug":"use-powershell-to-search-active-directory-users-missing-values-and-add-same","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-search-active-directory-users-missing-values-and-add-same\/","title":{"rendered":"Use PowerShell to Search Active Directory Users&#8217; Missing Values and Add Same"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong>: Learn how to use Windows PowerShell to search Microsoft Active Directory for users that have missing values, and automatically add default values.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" \/><\/p>\n<p> Hey, Scripting Guy! We have several network administrators where I work. Unfortunately, all of them have complete rights to Active Directory. This means that any one of the administrators can create a user. We have scripts that we are supposed to use when creating a user, but from time to time, one of the administrators will use Active Directory Users and Computers to manually create the user. When this happens, invariably something gets messed up. <\/p>\n<p>We just had an audit, and we got hit on the inspection due to the fact that our users are created in an inconsistent manner. We have 30 days to resolve our deficiencies before &ldquo;stuff&rdquo; hits the fan. What I need is a Windows PowerShell script that will search a particular Active Directory organizational unit for users that are missing a value for a specific attribute. After a user has been identified, I need the script to add a value for that attribute. Do you have a script that can perform this task for us?<\/p>\n<p>&#8212; CW<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" \/><\/p>\n<p>Hello CW, <\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. I keep waiting for things to slow down. It seems like it does not matter how many meetings one goes to, or how many emails one answers. There are always more coming down the pike. It is like &ldquo;honey do&rdquo; lists at home or lists of scripts I want to write. I have a whiteboard in my office that contains a list of scripts I want to write someday. It seemed like a good idea at the time. The scripts are all things that will take a bit of research to write. The problem is that script ideas come to the board faster than I can erase completed scripts to make room for more scripts. Scribbles cover the board; I may add a second board. <\/p>\n<p>CW, you are in luck because one of my whiteboard hieroglyphics represents the exact script scenario you detailed in your email to <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>. Actually, we are both in luck because I get to erase said hieroglyph from the board. The complete FindAndAddEmptyADValues.ps1 Windows PowerShell script is shown here.<\/p>\n<blockquote>\n<p class=\"CodeBlockScreenedHead\" style=\"line-height: 11pt;list-style-type: disc;margin: 15pt 12pt 0pt 0in;background: none transparent scroll repeat 0% 0%\"><span style=\"font-family: Arial\"><span style=\"font-size: 10pt;color: #000000\"><strong>FindAndAddEmptyADValues.ps1<\/strong><\/span><\/span><\/p>\n<div class=\"code\"><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-UsersFromOU<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #000000\">Param(<\/span><span style=\"color: #808080\">       <br \/>&nbsp; <\/span><span style=\"color: #2b91af\">$SearchRoot<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">       <br \/>&nbsp; <\/span><span style=\"color: #2b91af\">$filter<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;ObjectCategory=Person&#8221;       <br \/>&nbsp;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #2b91af\">$ds<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[adsiSearcher]<\/span><span style=\"color: #2b91af\">$filter<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #2b91af\">$ds<\/span><span style=\"color: #000000\">.SearchRoot<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[adsi]<\/span><span style=\"color: #808080\">&#8220;LDAP:\/\/$SearchRoot&#8221;       <br \/>&nbsp;<\/span><span style=\"color: #2b91af\">$ds<\/span><span style=\"color: #000000\">.findAll()<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">get-usersFromOu<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Set-AttributeValue<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #000000\">Param(<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #2b91af\">$SR<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #2b91af\">$SearchAttribute<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #2b91af\">$NewValue<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$null<\/span><span style=\"color: #808080\">       <br \/>&nbsp; <\/span><span style=\"color: #0000ff\">Foreach<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$user<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$sr<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <br \/>&nbsp; <\/span><span style=\"color: #0000ff\">IF<\/span><span style=\"color: #000000\">([string]::isNullOrEmpty(<\/span><span style=\"color: #2b91af\">$user<\/span><span style=\"color: #000000\">.properties.item(<\/span><span style=\"color: #2b91af\">$SearchAttribute<\/span><span style=\"color: #000000\">)))<\/span><span style=\"color: #808080\">       <br \/>&nbsp;&nbsp; <\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <br \/>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$de<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$user<\/span><span style=\"color: #000000\">.GetDirectoryEntry()<\/span><span style=\"color: #808080\">       <br \/>&nbsp;&nbsp;&nbsp;&nbsp; &#8220;modifying $($de.name)&#8221;        <br \/>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$de<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #2b91af\">$SearchAttribute<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$newValue<\/span><span style=\"color: #808080\">       <br \/>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$de<\/span><span style=\"color: #000000\">.setinfo()<\/span><span style=\"color: #808080\">       <br \/>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$i++<\/span><span style=\"color: #808080\">       <br \/>&nbsp;&nbsp; <\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">       <br \/>&nbsp; <\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">foreach-object<\/span><span style=\"color: #808080\">       <br \/>&nbsp; &#8220;modified $i users&#8221;        <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Set-AttributeValue<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">***<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Entry<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Point<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">to<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">script<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">***<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #2b91af\">$sr<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-UsersFromOU<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-SearchRoot<\/span><span style=\"color: #808080\"> &#8220;ou=testou,dc=nwtraders,dc=com&#8221;       <br \/><\/span><span style=\"color: #000000\">Set-AttributeValue<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-SR<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$sr<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-SearchAttribute<\/span><span style=\"color: #808080\"> &#8220;description&#8221; <\/span><span style=\"color: #000000\">-NewValue<\/span><span style=\"color: #808080\"> &#8220;By Script&#8221;<\/span> <\/div>\n<\/blockquote>\n<p>The FindAndAddEmptyADValues.ps1 begins with the <strong>Get-UsersFromOU<\/strong> function. The function accepts two parameters: the first parameter specifies the organizational unit (OU) to use for the search root, and the second parameter specifies the search filter. The search filter is set to filter out objects that are equal to the <strong>objectcategory<\/strong> of person. A filter such as this one will exclude computer objects. The parameter portion of the <strong>Get-UsersFromOU<\/strong> function is shown here: <\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-UsersFromOU<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #000000\">Param(<\/span><span style=\"color: #808080\">       <br \/>&nbsp; <\/span><span style=\"color: #2b91af\">$SearchRoot<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">       <br \/>&nbsp; <\/span><span style=\"color: #2b91af\">$filter<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;ObjectCategory=Person&#8221;       <br \/>&nbsp;<\/span><span style=\"color: #000000\">)<\/span> <\/div>\n<\/blockquote>\n<p>The <strong>[adsiSearcher]<\/strong> type accelerator is used to create an instance of the <strong>System.DirectoryServices.DirectorySearcher<\/strong> .NET Framework class. The search filter is supplied to the <strong>[adsiSearcher]<\/strong> type accelerator for its constructor. Other ways of creating an instance of the <strong>DirectorySearcher<\/strong> class are documented on MSDN. The <strong>DirectorySearcher<\/strong> class is created here and stored in the <strong>$ds<\/strong> variable:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$ds<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[adsiSearcher]<\/span><span style=\"color: #2b91af\">$filter<\/span> <\/div>\n<\/blockquote>\n<p>The <strong>searchRoot<\/strong> property of the <strong>DirectorySearcher<\/strong> needs to be an instance of a <strong>System.DirectoryServices.<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.directoryservices.directoryentry.aspx\">DirectoryEntry<\/a><\/strong> .NET Framework class. To easily create an instance of the <strong>DirectoryEntry<\/strong> class, use the <strong>[adsi]<\/strong> type accelerator as shown here: <\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$ds<\/span><span style=\"color: #000000\">.SearchRoot<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[adsi]<\/span><span style=\"color: #808080\">&#8220;LDAP:\/\/$SearchRoot&#8221;<\/span> <\/div>\n<\/blockquote>\n<p>To find all of the objects that meet the search filter and are located in the search root, use the <strong>findall()<\/strong> method shown here:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$ds<\/span><span style=\"color: #000000\">.findAll()<\/span> <\/div>\n<\/blockquote>\n<p>The complete <strong>Get-UsersFromOU<\/strong> function is shown here.<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-UsersFromOU<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #000000\">Param(<\/span><span style=\"color: #808080\">       <br \/>&nbsp; <\/span><span style=\"color: #2b91af\">$SearchRoot<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">       <br \/>&nbsp; <\/span><span style=\"color: #2b91af\">$filter<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;ObjectCategory=Person&#8221;       <br \/>&nbsp;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #2b91af\">$ds<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[adsiSearcher]<\/span><span style=\"color: #2b91af\">$filter<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #2b91af\">$ds<\/span><span style=\"color: #000000\">.SearchRoot<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[adsi]<\/span><span style=\"color: #808080\">&#8220;LDAP:\/\/$SearchRoot&#8221;       <br \/>&nbsp;<\/span><span style=\"color: #2b91af\">$ds<\/span><span style=\"color: #000000\">.findAll()<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">get-UsersFromOU<\/span> <\/div>\n<\/blockquote>\n<p>The <strong>Set-AttributeValue<\/strong> function accepts three input parameters. The first is an instance of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.directoryservices.searchresultcollection.aspx\">System.DirectoryServices.SearchResultCollection<\/a> class that is returned from the <strong>Get-UsersFromOU<\/strong> function. The <strong>$searchAttribute<\/strong> parameter is the attribute that is to be checked for a value, and a value is supplied if it is empty or null. The <strong>$newValue<\/strong> parameter is the string value to add to the empty or null attribute value. This portion of the <strong>Set-AttributeValue<\/strong> function is shown here:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Set-AttributeValue<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;<\/span><span style=\"color: #000000\">Param(<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;<\/span><span style=\"color: #2b91af\">$SR<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;<\/span><span style=\"color: #2b91af\">$SearchAttribute<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;<\/span><span style=\"color: #2b91af\">$NewValue<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">       <br \/><\/span><\/div>\n<\/blockquote>\n<p>The <strong>$i<\/strong> variable is used to count the number of users that are modified. It has its initial value set to <strong>$null<\/strong>. Because the <strong>$sr<\/strong> variable contains a <strong>SearchResultCollection<\/strong> class, I need to walk through the collection to retrieve an individual instance of the <strong>SearchResult<\/strong> class. The <strong>$user<\/strong> variable will contain a single instance of the <strong>SearchResult<\/strong> class on each pass through the collection. The <strong>isNullOrEmpty<\/strong> static method is used from the <strong>system.string<\/strong> .NET Framework class. This portion of the function is shown here:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$null<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp; <\/span><span style=\"color: #0000ff\">Foreach<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$user<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$sr<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp; <\/span><span style=\"color: #0000ff\">IF<\/span><span style=\"color: #000000\">([string]::isNullOrEmpty(<\/span><span style=\"color: #2b91af\">$user<\/span><span style=\"color: #000000\">.properties.item(<\/span><span style=\"color: #2b91af\">$SearchAttribute<\/span><span style=\"color: #000000\">)))<\/span><span style=\"color: #808080\">       <br \/><\/span><\/div>\n<\/blockquote>\n<p>The <strong>SearchResult<\/strong> class has a <strong>GetDirectoryEntry<\/strong> method that will create a <strong>DirectoryEntry<\/strong> class that matches the <strong>SearchResult<\/strong>. The attribute receives a new value, and the <strong>setinfo()<\/strong> method is called to write the changes back to Active Directory. The value of the <strong>$i<\/strong> counter variable is then incremented, and a summary string is displayed in the Windows PowerShell console. This portion of the function is shown here:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$de<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$user<\/span><span style=\"color: #000000\">.GetDirectoryEntry()<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp; &#8220;modifying $($de.name)&#8221;        <\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$de<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #2b91af\">$SearchAttribute<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$newValue<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$de<\/span><span style=\"color: #000000\">.setinfo()<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$i++<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp; <\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp; <\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">foreach-object<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp; &#8220;modified $i users&#8221;        <\/p>\n<p><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Set-AttributeValue<\/span><span style=\"color: #808080\">       <br \/><\/span><\/div>\n<\/blockquote>\n<p>The complete <strong>Set-AttributeValue<\/strong> function is shown here.<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Set-AttributeValue<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;<\/span><span style=\"color: #000000\">Param(<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;<\/span><span style=\"color: #000000\">$SR,<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;<\/span><span style=\"color: #000000\">$SearchAttribute,<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;<\/span><span style=\"color: #000000\">$NewValue<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;<\/span><span style=\"color: #000000\">$i<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">$<\/span><span style=\"color: #0000ff\">null<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp; <\/span><span style=\"color: #000000\">Foreach($user<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">$sr)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp; <\/span><span style=\"color: #000000\">IF([<\/span><span style=\"color: #0000ff\">string<\/span><span style=\"color: #000000\">]::isNullOrEmpty($user.properties.item($SearchAttribute)))<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp; <\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">$de<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">$user.GetDirectoryEntry()<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp; &#8220;modifying $($de.name)&#8221;        <\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">$de.$SearchAttribute<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">$newValue<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">$de.setinfo()<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">$i++<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp; <\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp; <\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">foreach<\/span><span style=\"color: #000000\">&#8211;<\/span><span style=\"color: #0000ff\">object<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp; &#8220;modified $i users&#8221;        <\/p>\n<p><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Set-AttributeValue<\/span><span style=\"color: #808080\">       <br \/><\/span><\/div>\n<\/blockquote>\n<p>The entry point to the FindAndAddEmptyAdValues.ps1 script calls the <strong>Get-UsersFromOU<\/strong> function and stores the returned <strong>SearchResultCollection<\/strong> in the <strong>$sr<\/strong> variable. It then calls the <strong>Set-AttributeValue<\/strong> function and passes the <strong>SearchResultCollection<\/strong>, an attribute to search, and a new value to supply if the attribute value is empty or null. This portion of the script is shown here:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">***<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Entry<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Point<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">to<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">script<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">***<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp; <\/p>\n<p><\/span><span style=\"color: #2b91af\">$sr<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-UsersFromOU<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-SearchRoot<\/span><span style=\"color: #808080\"> &#8220;ou=testou,dc=nwtraders,dc=com&#8221;       <\/p>\n<p><\/span><span style=\"color: #000000\">Set-AttributeValue<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-SR<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$sr<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-SearchAttribute<\/span><span style=\"color: #808080\"> &#8220;description&#8221; <\/span><span style=\"color: #000000\">-NewValue<\/span><span style=\"color: #808080\"> &#8220;By Script&#8221;       <br \/><\/span><\/div>\n<\/blockquote>\n<p>When I ran the script on my computer, the output shown in the following image appeared in my Windows PowerShell ISE.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8322.HSG-10-13-10-01_5F9D8C95.jpg\"><img decoding=\"async\" height=\"506\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5518.HSG-10-13-10-01_thumb_3A03E967.jpg\" alt=\"Image of output from running script\" border=\"0\" title=\"Image of output from running script\" style=\"border-bottom: 0px;border-left: 0px;margin:;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>CW, that is all there is to using Windows PowerShell to search AD DS for users with empty attribute values and then add values to those attributes. Active Directory Week will continue tomorrow when we will talk about using the Microsoft <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ee617195.aspx\">Active Directory cmdlets<\/a> to search AD DS for users with empty attribute values. <\/p>\n<p>We invite you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Summary: Learn how to use Windows PowerShell to search Microsoft Active Directory for users that have missing values, and automatically add default values. &nbsp; Hey, Scripting Guy! We have several network administrators where I work. Unfortunately, all of them have complete rights to Active Directory. This means that any one of the administrators can [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,3,8,20,45],"class_list":["post-16831","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-searching-active-directory","tag-user-accounts","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Summary: Learn how to use Windows PowerShell to search Microsoft Active Directory for users that have missing values, and automatically add default values. &nbsp; Hey, Scripting Guy! We have several network administrators where I work. Unfortunately, all of them have complete rights to Active Directory. This means that any one of the administrators can [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=16831"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16831\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=16831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=16831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=16831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}