How to regex a table output RRS feed

  • Question

  • Hey guys,

    So I need a script that runs on a client machine to get the person's username and then, based on that, his OU in AD. This is what I have thus far:

    $userProperties=Get-ADUser $username
    Add-Content $pathOutUser $userProperties
    $userPath = Resolve-Path "C:\Scripts\UsAer_Logger\Output\userTemp\$username.txt"
    if (Test-Path $userPath)
      $userParse = [regex]::Match($txt,'OU=(.*)').Groups[1].Value

    The output of Get-AdUser $username looks like this:

    DistinguishedName : CN=Alexander Bazzi,OU=IT,OU=Coral Gables,OU=US,DC=corp,DC=aersale,DC=com
    Enabled           : True
    GivenName         : Alexander
    Name              : Alexander Bazzi
    ObjectClass       : user
    ObjectGUID        : 96b6b7a2-03d1-4275-b6dc-9239a2774275
    SamAccountName    : abazzi
    SID               : S-1-5-21-3137774707-1406588056-219970087-3909
    Surname           : Bazzi
    UserPrincipalName :

    What I need is a way to parse through this text and get the OU of the user (IT, in this case). Am I in the right track? How would you do it? Is there a way to do it in the pipe, without having to save the table to a text file and then read it?

    Many thanks in advance,


    Wednesday, July 2, 2014 2:03 PM


All replies