Marsupilami74
Marsupilami74

Reputation: 1

How to filter Get-ADComputer output

My Get-ADComputer script gives too much information. I would like to shorten it out a little.

$Computer = Read-Host -Prompt 'Input computer name'
$ManagedBy = Get-ADComputer $Computer -Properties ManagedBy |
             foreach { $_.ManagedBy }
Write-Output $ManagedBy

When I tried to run my scrip it gives this to output

CN=Last Name First Name ,OU=XX ,OU=XXX ,OU=XXX ,DC=XXX,DC=XXX

I would like to get only CN in the output (First name and Las Name).

Upvotes: 0

Views: 584

Answers (2)

yaquaholic
yaquaholic

Reputation: 152

Firstly have you looked at the objects properties? These Properties are auto assigned to the variable, when created.

You can see them with:

$ManagedBy | Get-Member

You may well find that $ManagedBy.Name will give exactly what you want.

Further reading for you: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-member?view=powershell-6

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

Your code returns the distinguished name of the computer's manager. You can use that DN to query the AD user object and obtain the desired properties from that (like FullName, or DisplayName, or the individual values FirstName and LastName).

Get-ADComputer $Computer -Properties ManagedBy |
    Select-Object -Expand ManagedBy |
    Get-ADUser -Property FullName |
    Select-Object -Expand FullName

Upvotes: 2

Related Questions