Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

AD Query for specific set of user attributes and formatting output

$
0
0

just getting started with powershell and read the following technet article http://technet.microsoft.com/en-us/library/ff730967.aspx

I have modified the script from the page above to find what I want it to find.   I am searching for Users where the Job Title field in there AD account=undergrad.   The query is successful and I have also modified to send the output to a file.  My output is listed in successive lines, I am pulling the users name and accountexpires and they show up like:

user1

861681618916116851

user2

1864496589448646464

and so on.

I want both the user and account expiration date to be on one line in my output and separated by a comma, a csv if you will.  I also would like to convert the date format to a human readable format (I could probably do the date conversion in excel after the data is imported but it would be nice to know if I can do the conversion within PS.   If someone is feeling really helpful, I would also like to exclude disabled accounts, which would be a sweet deal all around.

What I am trying to achieve would look more like

User1,4/31/2014

User2,3/24/2015

Here is the complete script I have now.

$strFilter = "(&(objectClass=User)(Title=undergrad))"

$objOU = New-Object System.DirectoryServices.DirectoryEntry

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"

$colProplist = "name","accountExpires"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

$colResults = $objSearcher.FindAll()

$(foreach ($objResult in $colResults)
    {$objItem = $objResult.Properties
      $objItem.name
      $objItem.accountexpires
    }
  ) | Out-File 'c:\PS\unders.txt'

Thanks in advance for any tips on this.


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>