Run PS Script on Specified OU

This topic contains 3 replies, has 3 voices, and was last updated by  Flynn Bundy 2 years, 5 months ago.

  • Author
    Posts
  • #27092

    Rackstar
    Participant

    Hi

    Im wondering if anyone could help me running this PS script on a specified OU?

    https://gallery.technet.microsoft.com/office/Sync-Primary-Email-Address-a1d989cb#content

    Can i pipe it some how?

    Thanks

  • #27095

    Richard Siddaway
    Moderator

    You should be able to use the -searchBase parameter on Get-AdObject to define the OU you want to use – it needs the distinguished name of the OU

  • #27180

    Rackstar
    Participant

    Thanks Richard. I haven't much powershell experience. Would you be able to show an example of how it should be constructed please?

  • #27182

    Flynn Bundy
    Participant

    Hi Rackstar,

    What Richard is saying is that you can select whichever machines you wish by specifying the distinguished name of the OU. As an example

    $computers = Get-ADComputer -SearchBase "OU=DSC,DC=Overlord,DC=Corp,DC=Com" -Filter * | select -ExpandProperty Name 

    This would go to my DSC OU and gather all of the names of the machines in that OU and store them away in my computers variable. From here you have a collection of computers stored in a variable and you can do whatever you like to do. for example:

    Foreach ($Item in $computers){
    
    Write-Information "Hello There"
    
    }

You must be logged in to reply to this topic.