none
PowerShell Find all OU with group folder named Terminations RRS feed

  • Question

  • Writing a term script and need to find all OU's with the sub folder of Terminations. The following script below works, but will only return the OU when the complete path is defined. I want to do a wild card search find all OU's with terminations. 

    #Get-ADOrganizationalUnit -filter {(name -like "*terminations*")} |fl name, distinguishedname

    #returns all OU with terminations but does not return the members of that ou. 

    #returns defined path of an OU and all its members

    $password = ConvertTo-SecureString -AsPlainText "Here15448" -Force 

    $users = Get-ADUser -Filter * -SearchBase "OU=Terminations,OU=NYNYC,DC=corp,DC=local" 


    foreach($user in $users)
    { set-adaccountpassword -id $user -newpassword $password -reset -whatif
      disable-adaccount -id $user
    }


    Aaron Harris ExchangeSharePointGuy

    Monday, November 3, 2014 4:06 PM

Answers

  • @Bill

    I think I found a solution to my questions. 

    $users = Get-ADUser -Filter * -SearchBase "dc=domain,dc=com"
    $Filtered = $Users |Where-Object {$_.distinguishedName -match "OU=Terminations"}


    foreach($user in $Filtered)

    set-adaccountpassword -id $user -newpassword $password -reset -whatif
      disable-adaccount -id $user
     set-aduser -id $user -manager $null


    }



    Aaron Harris ExchangeSharePointGuy

    Monday, November 3, 2014 7:01 PM

All replies

  • You cannot use wildcard matching for OU names in a query.

    The only thing you can do is search for attributes and filter based on the results.


    -- Bill Stewart [Bill_Stewart]

    Monday, November 3, 2014 4:12 PM
    Moderator
  • @Bill

    I think I found a solution to my questions. 

    $users = Get-ADUser -Filter * -SearchBase "dc=domain,dc=com"
    $Filtered = $Users |Where-Object {$_.distinguishedName -match "OU=Terminations"}


    foreach($user in $Filtered)

    set-adaccountpassword -id $user -newpassword $password -reset -whatif
      disable-adaccount -id $user
     set-aduser -id $user -manager $null


    }



    Aaron Harris ExchangeSharePointGuy

    Monday, November 3, 2014 7:01 PM
  • Correct. Where-Object filters after you've already run the query.

    -- Bill Stewart [Bill_Stewart]

    Monday, November 3, 2014 7:16 PM
    Moderator