locked
Move computers to ou RRS feed

  • Question

  • Hello I am trying to create a powershell script that will move computers from an ou to another ou that looks through a notepad list.

    Here is what I have but cannot figure out what im missing

    $searchb = "CN=Computers,DC=Company,DC=COM"
    $content = get-content C:\users\test.txt
    Get-ADComputer -SearchBase $searchb -filter $content -ea 0 | Move-AdObject -TargetPath 'OU=Test,OU=Computers,OU=Location,DC=Company,DC=COM'

    I started from this script but it would move all computers in the ou

    $TestComputers = Get-AdComputer -filter * -SearchBase "CN=Computers,DC=Company,DC=COM"
    get-content C:\test.txt | "$TestComputers" -ea 0 | Move-AdObject -TargetPath 'OU=Test,OU=Computers,OU=Location,DC=Company,DC=COM'

    Hopefully someone can point out what im missing as im supposed to move a list of 300 computers to another ou this afternoon and would rather not do it myself. 

    Ultimately i have a list of computers in multiple ou's so ill be changing this script for each Location

    Thank you,

    Adam


    Saturday, April 14, 2018 3:45 PM

Answers

  • Or you can do this:

    Get-Content c:\test.txt | 
        Get-AdComputer |
        Where{$_.DistinguishedNAme -match 'CN=Computers,DC=Company,DC=COM'} |
        Move-AdObject -targetpath 'OU=Test,OU=Computers,OU=Location,DC=Company,DC=COM'


    \_(ツ)_/

    • Marked as answer by Gr33ngoo Saturday, April 14, 2018 7:41 PM
    Saturday, April 14, 2018 7:32 PM

All replies

  • Hi Gr33ngoo

    Try this:

    get-content c:\test.txt | % {Get-ADComputer -Identity $_  -properties * |move-adobject -targetpath 'OU=Test,OU=Computers,OU=Location,DC=Company,DC=COM'}

    Regards

    Simon


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. Regards Simon Disclaimer: This posting is provided AS IS with no warranties or guarantees, and confers no rights.

    Saturday, April 14, 2018 4:11 PM
  • With a file of computer names this is all you need to do:

    Get-Content c:\test.txt | 
    Get-AdComputer |
    Move-AdObject -targetpath 'OU=Test,OU=Computers,OU=Location,DC=Company,DC=COM'

    There is no need to test the computers as the computer account can be moved even if the computer is turned off. It is an AD only process.

    PowerShell and the AD CmdLets take care of everything else.


    \_(ツ)_/

    Saturday, April 14, 2018 5:18 PM
  • Note also that your file can contain the computer name, SamAccountName, DistinguishedName or GUID.  Any of these will work correctly.


    \_(ツ)_/

    Saturday, April 14, 2018 5:28 PM
  • This command would work my problem however is I am wanting to take a computer from a specific ou.

    So I have a list of 100 computers maybe 20 of those are in the original ou. 

    I want to move those 20 computers to the target path. While leaving the rest in the original location. 

    ie I have 20 computers in path not sure which ones are in the text document they are located in 

    OU=Location,DC=Company,DC=COM

    I want to move those 20 computers to OU=Test,OU=Computers,OU=Location,DC=Company,DC=COM

    I will then run this account at the location OU=Location2,DC=Company,DC=COM and move them to that locations Test Folder.

    Thanks,

    Saturday, April 14, 2018 6:04 PM
  • The computers will only come from the correct OU. A computer name is unique across a domain.

    If you are sayin that you only want to move the computers in the list that are in a specific OU then use the SearchBase of the Get-AdComputer.


    \_(ツ)_/

    Saturday, April 14, 2018 7:26 PM
  • Or you can do this:

    Get-Content c:\test.txt | 
        Get-AdComputer |
        Where{$_.DistinguishedNAme -match 'CN=Computers,DC=Company,DC=COM'} |
        Move-AdObject -targetpath 'OU=Test,OU=Computers,OU=Location,DC=Company,DC=COM'


    \_(ツ)_/

    • Marked as answer by Gr33ngoo Saturday, April 14, 2018 7:41 PM
    Saturday, April 14, 2018 7:32 PM
  • Thanks that is exactly what I was looking for I was trying the Searchbase originally and that is where it was failing.
    Saturday, April 14, 2018 7:42 PM