Get Computer Name by Description

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Brad Brad 4 months, 2 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #39037
    Profile photo of Brad
    Brad
    Participant

    Hi,
    I try get computer name by description
    but it not working correct
    it get just last name from my list
    This my code:

    $username = Get-Content "C:\Users.txt"

    foreach ($entry in $username)
    {
    Get-ADComputer -Filter ("Description -Like '$entry'") -Property Name,Description |
    Select Name,Description |
    Export-Csv "C:\CompName.csv" -NoTypeInformation
    }

    #39042
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You're calling Export-Csv inside a loop without the -Append switch, which will just keep overwriting the file. One alternative is to turn this into a single pipeline with Export-Csv at the end (so it's only called once, but receives multiple input objects):

    Get-Content "C:\Users.txt" |
    Get-ADComputer -Filter ("Description -Like '$_'") -Property Name,Description |
    Select Name,Description |
    Export-Csv "C:\CompName.csv" -NoTypeInformation
    
    #39047
    Profile photo of Brad
    Brad
    Participant

    Hi Dave,

    Thank you for replay, the -Append work

    Thanks.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.