Get PC Names with Managed By Attribute

create share 676 Reputation points
2023-11-26T21:16:23.36+00:00

Hi,

Is it possible to export a list of PCs with "Managed By" attribute from a specific OU?

Thanks.

Windows for business | Windows Client for IT Pros | Directory services | Active Directory
0 comments No comments
{count} votes

Accepted answer
  1. gastone canali 236 Reputation points
    2023-11-26T22:25:04.0166667+00:00

    in powershell

    # Import the Active Directory module
    Import-Module ActiveDirectory
    
    # Specify the distinguished name of the OU (Organizational Unit) where your computers are located
    $ou = "OU=Computers,DC=yourdomain,DC=com"
    
    # Get a list of computers with the "Managed By" attribute
    $computers = Get-ADComputer -Filter * -SearchBase $ou -Properties ManagedBy | Where-Object { $_.ManagedBy }
    
    # Display the computer names and their corresponding "Managed By" users
    foreach ($computer in $computers) {
        $computerName = $computer.Name
        $managedBy = Get-ADUser -Identity $computer.ManagedBy
        $managedByName = $managedBy.Name
    
        Write-Output "Computer: $computerName - Managed By: $managedByName"
    }
    
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. create share 676 Reputation points
    2023-12-04T09:09:21.1633333+00:00
    Did not work with me. Getting the below error. Am i doing a mistake?
    
    Get-ADComputer : The object name has bad syntax
    At C:\scripts\Managedby.ps1:8 char:14
    + ... computers = Get-ADComputer -Filter * -SearchBase $ou -Properties Mana ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Get-ADComputer], ADException
        + FullyQualifiedErrorId : ActiveDirectoryServer:8335,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
    
    0 comments No comments

  2. Thameur-BOURBITA 36,261 Reputation points Moderator
    2023-12-04T20:23:40+00:00

    Hi @create share

    I think on you script you didn't specify the path of target OU. Please check on your script if the path is correct , if it's not the case replace "OU=Computers,DC=yourdomain,DC=com" by the correct OU in your domain.


    Please don't forget to accept helpful answer


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.