# Get-adcomputer

**URL:** <https://forums.powershell.org/t/get-adcomputer/14768>\
**Category:** PowerShell Help\
**Created:** [August 7, 2020, 1:26am UTC](https://forums.powershell.org/t/get-adcomputer/14768 "2020-08-07T01:26:31Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![ranjithckm7](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ranjithckm7/32/2044_2.png) [@ranjithckm7](https://forums.powershell.org/u/ranjithckm7)\
**Post date:** [August 7, 2020, 1:26am UTC](https://forums.powershell.org/t/get-adcomputer/14768/1 "2020-08-07T01:26:31Z")

</div>

Hi,

I’m new to this forum and beginner to Powershell too and not sure if it’s right forum to get some help on my query.

I believe i will not be a beginner like now if i keep reading and practicing from this space. 😉

Not sure what’s wrong here but it returns nothing. ☹

&nbsp;

```
```
$computers=Get-Content "C:\test\Input\Computers.txt"
$computers|ForEach-Object -Process {Get-ADComputer -filter 'Name -eq "$_"' -Properties *|select -Property Name,OperatingSystem}
```
```

---

<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 7, 2020, 1:36am UTC](https://forums.powershell.org/t/get-adcomputer/14768/2 "2020-08-07T01:36:05Z")

</div>

Ranjithkumar

You switched the quotation marks. Try this:

```auto
$computers = Get-Content "C:\test\Input\Computers.txt"
$computers | 
ForEach-Object -Process { 
    Get-ADComputer -filter "Name -eq '$_'" -Properties Operatingsystem 
}

```

You might consider another loop type …

```auto
$computerList = Get-Content "C:\test\Input\Computers.txt"
foreach ($ComputerName in $computerList) {
    Get-ADComputer -Identity $ComputerName -Properties Operatingsystem
}

```

As the attribute “Name” already is part of the default subset Get-ADcomputer returns you don’t have to specify it explicitly for -Properties. 😉

---

<div class="post-metadata">

**Author:** ![ranjithckm7](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ranjithckm7/32/2044_2.png) [@ranjithckm7](https://forums.powershell.org/u/ranjithckm7)\
**Post date:** [August 7, 2020, 2:09am UTC](https://forums.powershell.org/t/get-adcomputer/14768/3 "2020-08-07T02:09:37Z")

</div>

It’s awesome. Thanks for pointing this out. It worked.

And you was very fast too. 🙂

---

<div class="post-metadata">

**Author:** ![ranjithckm7](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ranjithckm7/32/2044_2.png) [@ranjithckm7](https://forums.powershell.org/u/ranjithckm7)\
**Post date:** [August 7, 2020, 2:28am UTC](https://forums.powershell.org/t/get-adcomputer/14768/4 "2020-08-07T02:28:37Z")

</div>

Okay. Here I’ve come with another one very fast.

While I’m entering many computer names i.e. 80 Computers, It returns error as

&nbsp;

> Get-ADComputer : Cannot find an object with identity: '\<Computername\> ' under: '\<DomainName\>'.

I have tried keying in -searchbase, -searchscope and -server too.
```
$computerList = Get-Content "C:\test\Input\Computers.txt"
foreach ($ComputerName in $computerList) {
Get-ADComputer -Identity $ComputerName -Properties Operatingsystem -searchbase "<domainname>" -SearchScope Subtree -Server <DCName> |
Select -Property Name,OperatingSystem
}
```

---

<div class="post-metadata">

**Author:** ![ranjithckm7](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ranjithckm7/32/2044_2.png) [@ranjithckm7](https://forums.powershell.org/u/ranjithckm7)\
**Post date:** [August 7, 2020, 2:45am UTC](https://forums.powershell.org/t/get-adcomputer/14768/5 "2020-08-07T02:45:19Z")

</div>

Sorry. I think. I have got this. There were extra spaces on each computer names. That was the issue.

---

<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 7, 2020, 2:48am UTC](https://forums.powershell.org/t/get-adcomputer/14768/6 "2020-08-07T02:48:48Z")

</div>

> [@](#):
>
> Get-ADComputer : Cannot find an object with identity: ‘\<Computername\> ‘ under: ‘\<DomainName\>’.

I’d suspect that you have computer names in your list that are not in your AD - maybe typos. Can you check this?

---

<div class="post-metadata">

**Author:** ![panzerbjrn](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/panzerbjrn/32/36_2.png) [@panzerbjrn](https://forums.powershell.org/u/panzerbjrn)\
**Post date:** [August 7, 2020, 3:01am UTC](https://forums.powershell.org/t/get-adcomputer/14768/7 "2020-08-07T03:01:52Z")

</div>

~~Does it work if you run the Get-ADComputer command using one of the names from the list?  
Get-ADComputer -filter ‘Name -eq “ServerA”’ -Properties \*|select -Property Name,OperatingSystem -verbose~~

What happens if you pick a name from the list by doing this:  
`$computers[0]|ForEach-Object -Process {Get-ADComputer -filter 'Name -eq "$_"' -Properties *|select -Property Name,OperatingSystem -verbose}`

Hah hah, I guess I should have refreshed after having the tab open for a bit…

---

<div class="post-metadata">

**Author:** ![ranjithckm7](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ranjithckm7/32/2044_2.png) [@ranjithckm7](https://forums.powershell.org/u/ranjithckm7)\
**Post date:** [August 7, 2020, 3:11am UTC](https://forums.powershell.org/t/get-adcomputer/14768/8 "2020-08-07T03:11:56Z")

</div>

Here’s the next stop. To get the output as csv file. It generates header names for each computer result.  
i.e. like below.

> Name OperatingSystem ---- --------------- Computername Windows 10 Enterprise
> 
> Name OperatingSystem
> 
> * * *
> 
> Computername Windows 10 Enterprise
> 
> Name OperatingSystem
> 
> * * *
> 
> ComputernameWindows 10 Enterprise
> 
> Name OperatingSystem
> 
> * * *
> 
> Computername Windows 7 Enterprise
> 
> Name OperatingSystem
> 
> * * *
> 
> Computername Windows 10 Enterprise

  
Updated script

```
$computerList = Get-Content "C:\test\Input\Computers.txt"
foreach ($ComputerName in $computerList) {
Try{
$output=Get-ADComputer -Identity $ComputerName -Properties Operatingsystem|
Select -Property Name,OperatingSystem|Out-File -FilePath C:\test\Output\Result.csv -Append

}
catch{
Write-Output "$_.name"|Out-File -FilePath C:\test\Output\Result.csv -Append
}
}
```

---

<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 7, 2020, 3:19am UTC](https://forums.powershell.org/t/get-adcomputer/14768/9 "2020-08-07T03:19:44Z")

</div>

Use `Export-Csv -Path C:\test\Output\Result.csv -Notype` rather than `Out-File -FilePath C:\test\Output\Result.csv -Append`.

`Export-Csv` will set the first row as the header row. The remaining rows will be data. `Export-Csv -Delimiter 'character'` allows for a different delimiter (`character`) than the default comma.

---

<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 7, 2020, 3:23am UTC](https://forums.powershell.org/t/get-adcomputer/14768/10 "2020-08-07T03:23:50Z")

</div>

```auto
$computerList = Get-Content "C:\test\Input\Computers.txt"
foreach ($ComputerName in $computerList) {
    Try {
        Get-ADComputer -Identity $ComputerName -Properties Operatingsystem -ErrorAction Stop |
        Select-Object -Property Name, OperatingSystem | Export-Csv -Path C:\test\Output\Result.csv -Append

    }
    catch {
        $ComputerName | Out-File -FilePath C:\test\Output\UnAvailable.txt -Append
    }
}

```

You may make a step back and keep learning the basics of Powershell a little further. 😉

---

<div class="post-metadata">

**Author:** ![ranjithckm7](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ranjithckm7/32/2044_2.png) [@ranjithckm7](https://forums.powershell.org/u/ranjithckm7)\
**Post date:** [August 7, 2020, 5:06am UTC](https://forums.powershell.org/t/get-adcomputer/14768/11 "2020-08-07T05:06:52Z")

</div>

> [@](#):
>
> > [Ranjithkumar Duraisamy wrote:](https://powershell.org/forums/topic/get-adcomputer/#post-247807)
> > Get-ADComputer : Cannot find an object with identity: ‘\<Computername\> ‘ under: ‘\<DomainName\>’.
> 
> I’d suspect that you have computer names in your list that are not in your AD – maybe typos. Can you check this?

Yes. You’re right. There were spaces at end of each computer names while i copied them from an excel.

---

<div class="post-metadata">

**Author:** ![ranjithckm7](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ranjithckm7/32/2044_2.png) [@ranjithckm7](https://forums.powershell.org/u/ranjithckm7)\
**Post date:** [August 7, 2020, 5:07am UTC](https://forums.powershell.org/t/get-adcomputer/14768/12 "2020-08-07T05:07:49Z")

</div>

[quote quote=247822] ~~Does it work if you run the Get-ADComputer command using one of the names from the list?~~

Get-ADComputer -filter ‘Name -eq “ServerA”‘ -Properties \*|select -Property Name,OperatingSystem -verbose

What happens if you pick a name from the list by doing this:

`$computers[0]|ForEach-Object -Process {Get-ADComputer -filter ‘Name -eq “$_”’ -Properties *|select -Property Name,OperatingSystem -verbose}`

Hah hah, I guess I should have refreshed after having the tab open for a bit…

[/quote]  
NP. Thanks for your helping mind. 🙂

---

<div class="post-metadata">

**Author:** ![ranjithckm7](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ranjithckm7/32/2044_2.png) [@ranjithckm7](https://forums.powershell.org/u/ranjithckm7)\
**Post date:** [August 7, 2020, 5:08am UTC](https://forums.powershell.org/t/get-adcomputer/14768/13 "2020-08-07T05:08:35Z")

</div>

[quote quote=247837]Use `Export-Csv -Path C:\test\Output\Result.csv -Notype` rather than `Out-File -FilePath C:\test\Output\Result.csv -Append`.

`Export-Csv` will set the first row as the header row. The remaining rows will be data. `Export-Csv -Delimiter ‘character’` allows for a different delimiter (`character`) than the default comma.

[/quote]  
Thanks much. I’ll use the same as suggested.

---

<div class="post-metadata">

**Author:** ![ranjithckm7](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ranjithckm7/32/2044_2.png) [@ranjithckm7](https://forums.powershell.org/u/ranjithckm7)\
**Post date:** [August 7, 2020, 5:15am UTC](https://forums.powershell.org/t/get-adcomputer/14768/14 "2020-08-07T05:15:54Z")

</div>

[quote quote=247843]`$computerList = Get-Content “C:\test\Input\Computers.txt”
foreach ($ComputerName in $computerList) {
Try {
Get-ADComputer -Identity $ComputerName -Properties Operatingsystem -ErrorAction Stop |
Select-Object -Property Name, OperatingSystem | Export-Csv -Path C:\test\Output\Result.csv -Append`

`
```
}
catch {
    $ComputerName | Out-File -FilePath C:\test\Output\UnAvailable.txt -Append
}

```
`

`}` You may make a step back and keep learning the basics of Powershell a little further. 😉

[/quote]  
Thanks @Olaf.

Actually I keep on watching different lectures from Youtube(Don Jones) & LinkedLearning(Jason Helmick, Matt Hester, Adam Bertram). Due to lack of practice i end up in different or wrong commands rather than the appropriate one. But Thanks for you kind advise, I’ll continue to do that. 🙂

If you could share any PowerShell learning path to move from beginner, that would be really great…!! Thanks in advance. 🙂 Have a good weekend.

&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 7, 2020, 5:29am UTC](https://forums.powershell.org/t/get-adcomputer/14768/15 "2020-08-07T05:29:45Z")

</div>

[quote quote=247873]Actually I keep on watching different lectures from Youtube(Don Jones) & LinkedLearning(Jason Helmick, Matt Hester, Adam Bertram). Due to lack of practice i end up in different or wrong commands rather than the appropriate one. But Thanks for you kind advise, I’ll continue to do that. 🙂

If you could share any PowerShell learning path to move from beginner, that would be really great…!! Thanks in advance. 🙂 Have a good weekend.[/quote]

I think you’re doing just fine. What I’d like to recommend though is to search for a solution first before you ask in forum like this. Most questions a beginner has have been aswered already in the past. 😉

> [@](#):
>
> Yes. You’re right. There were spaces at end of each computer names while i copied them from an excel.

If you have to use a source like this and you cannot count on the quality of the source data you could try to use the .trim() method to remove spaces (or other unwnated charachters) from strings you need to use. Example:

```auto
$ComputerList = 
@(
    'Computer_01'
    ' Computer_02 '
    'Computer_03 '
    ' Computer_04'
)
foreach ($ComputerName in $ComputerList) {
    "Output without leading or trailing spaces: '$($ComputerName.trim())'"
}

```

---

<div class="post-metadata">

**Author:** ![ranjithckm7](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ranjithckm7/32/2044_2.png) [@ranjithckm7](https://forums.powershell.org/u/ranjithckm7)\
**Post date:** [August 7, 2020, 6:06am UTC](https://forums.powershell.org/t/get-adcomputer/14768/16 "2020-08-07T06:06:48Z")

</div>

Thanks @olaf

---

<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/get-adcomputer/14768/17 "2024-05-16T20:29:06Z")

</div>


