# Hopefully easy \> Computer Name from User Name (distinguishedName)

**URL:** <https://forums.powershell.org/t/hopefully-easy-computer-name-from-user-name-distinguishedname/14755>\
**Category:** PowerShell Help\
**Created:** [August 4, 2020, 4:14pm UTC](https://forums.powershell.org/t/hopefully-easy-computer-name-from-user-name-distinguishedname/14755 "2020-08-04T16:14:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonm](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@simonm](https://forums.powershell.org/u/simonm)\
**Post date:** [August 4, 2020, 4:14pm UTC](https://forums.powershell.org/t/hopefully-easy-computer-name-from-user-name-distinguishedname/14755/1 "2020-08-04T16:14:51Z")

</div>

Want to get the Computer name of the User (User account I know).

Hoping this makes sense below:

```
$User = Read-Host "Whats the Account UserName?"
#1. input Username credentials
get-aduser $User | Select-Object DistinguishedName
#2. get the distinguishedname for the user account
Get-ADComputer -Filter {ManagedBy -eq 'CN=xsurname, xfirst,OU=CG,OU=PROD,OU=BusinessUnits,DC=ourbusiness,DC=com,DC=au'
# 3. need to drop 'DistinguishedName" from 2. into the 3. [ManagedBy -eq {ManagedBy -eq 'xx....xxx' variable.
```

#How can I ‘define’ this as what the error is?

&nbsp;

Thanks in advance for this one - will save me plenty of time.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 4, 2020, 9:20pm UTC](https://forums.powershell.org/t/hopefully-easy-computer-name-from-user-name-distinguishedname/14755/2 "2020-08-04T21:20:04Z")

</div>

Simon, welcome to [Powershell.org](http://Powershell.org). Please take a moment and read the very first post on top of the list of this forum: [Read Me Before Posting! You’ll be Glad You Did!](https://powershell.org/forums/topic/read-me-before-posting-youll-be-glad-you-did).

When you post code, error messages, sample data or console output format it as code, please.  
In the “ **Text** ” view you can use the code tags “ **CODE** ”, in the “ **Visual** ” view you can use the format template “ **Preformatted** ”. You can go back edit your post and fix the formatting - you don’t have to create a new one.  
Thanks in advance.

> [@](#):
>
> # 3. need to drop ‘DistinguishedName” from 2. into the 3. [ManagedBy -eq {ManagedBy -eq ‘xx….xxx’ variable.

So you have to save the result of Get-ADUser in a variable and use it for the filter for Get-ADComputer.

> [@](#):
>
> #How can I ‘define’ this as what the error is?.

What do you mean with that? What Error?

---

<div class="post-metadata">

**Author:** ![adminofthings](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@adminofthings](https://forums.powershell.org/u/adminofthings)\
**Post date:** [August 5, 2020, 8:12am UTC](https://forums.powershell.org/t/hopefully-easy-computer-name-from-user-name-distinguishedname/14755/3 "2020-08-05T08:12:33Z")

</div>

The following should work:

```
$DN = $null # Clear out variable DN $user = Read-Host "What is the account username?" # -Expand returns the property value rather than an object containing the property $DN = Get-ADUser $user | Select-Object -Expand DistinguishedName # DN contains just the DistinguishedName Get-ADComputer -Filter 'ManagedBy -eq $DN'
```

---

<div class="post-metadata">

**Author:** ![simonm](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@simonm](https://forums.powershell.org/u/simonm)\
**Post date:** [August 5, 2020, 12:38pm UTC](https://forums.powershell.org/t/hopefully-easy-computer-name-from-user-name-distinguishedname/14755/4 "2020-08-05T12:38:03Z")

</div>

@AdminOfThings45 && Thanks for the feedback and working answer.

Most important thanks for the explanation and notes - learning for the day.

The script above does exactly as requested. Initially a few time outs however still produced result.

&nbsp;

For anyone else I added a new first line to the above to assist in my environment:

Import-Module ActiveDirectory

&nbsp;

Top of the day to you.

&nbsp;

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 5, 2020, 1:37pm UTC](https://forums.powershell.org/t/hopefully-easy-computer-name-from-user-name-distinguishedname/14755/5 "2020-08-05T13:37:56Z")

</div>

[quote quote=247345]For anyone else I added a new first line to the above to assist in my environment:

Import-Module ActiveDirectory[/quote]

Since Powershell version 3 that’s unnecessary. A properly installed module will be imported automatically by Powershell if needed.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/hopefully-easy-computer-name-from-user-name-distinguishedname/14755/6 "2024-05-16T20:29:05Z")

</div>


