PowerShell script to search computer in the Entire Directory.

Anonymous
2024-06-28T04:23:33+00:00

Please help me with the PowerShell script which I can run to search the computer in entire Active Directory.

And I am not part of that domain I have to change the domain in GUI of Active Directory Users and Computers and there is various domain in that domain, so I have to change the selection from 'In' Combo Box from a particular domain to the entire directory option. So, I want to do the same thing, I want script to search the computer in Entire Directory.

Windows Server Identity and access Active Directory

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question. To protect privacy, user profiles for migrated questions are anonymized.

0 comments No comments
{count} votes
Accepted answer
  1. Anonymous
    2024-06-28T07:05:35+00:00

    Hi SHARMA Ashutosh (HELLA),

    Thank you for posting in the Microsoft Community Forums.

    If you're not part of a domain, you'll need to access AD through an LDAP query or other authentication mechanism. This usually means that you need to provide valid domain credentials to query AD.

     # Import the AD module if it's not already loaded   
    
    Import-Module ActiveDirectory   
    
    # Set your domain credentials   
    
    # Note: In real-world usage, you shouldn't hardcode credentials but use a more secure method to obtain them   
    
    $username = "DOMAIN\username"   
    
    $password = ConvertTo-SecureString "yourpassword" -AsPlainText -Force   
    
    $credential = New-Object System.Management.Automation.PSCredential($username, $password)   
    
    # Connect to the root domain of the AD forest (or you can specify a specific domain controller)   
    
    # Note: Here we assume you have sufficient permissions to query the entire forest   
    
    $forestRootDomain = (Get-AdForest).RootDomain   
    
    # Set the search base to the root domain of the forest   
    
    $searchBase = "LDAP://CN=$forestRootDomain,$((Get-AdRootDSEntry).distinguishedName)"   
    
    # Perform the search, for example, searching for all computers   
    
    $filter = "(&(objectClass=computer))"   
    
    $computers = Get-ADObject -Filter $filter -SearchBase $searchBase -SearchScope Subtree -Credential $credential -Properties *   
    
    # Iterate through and output the computer information   
    
    foreach ($computer in $computers) {   
    
        $computer.Name   
    
        # You can add other properties as needed, such as $computer.DNSHostName, $computer.OperatingSystem, etc.   
    
    }
    

     In the script above, please replace the $username and $password variables with your domain credentials.

    Best regards

    Neuvi Jiang

    0 comments No comments

0 additional answers

Sort by: Most helpful