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