How to extrat/sort/output data from a power shell variable

Chris Huang 1 Reputation point
2021-09-20T12:54:08.917+00:00

Hi team,

I am new to powershell. Some help please.

I am trying to get a list of hostname from an OU which contains only computer object, I would like to sort the rows of hostname and only output a smaller list of hostname which contains certain keywords (Sales_PC1, Sales_PC2 etc...) to a txt file.

$PCList = Get-ADComputer -SearchBase 'OU=MyComputerOU, dc=contoso, dc=co, dc=nz' -Filter '*' |Select -Expand Name

The above command returns all the computer objects that are sitting in the "MyComputerOU".

What PowerShell command should I use to extract data from the variable $PCList and output it to a txt file?

Your help would be much appreciated.

Thanks,
Chris

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,091 Reputation points
    2021-09-20T14:43:06.887+00:00

    A slight modification of the example posted by @Andreas Baumgarten .

    There's no need to create an intermediate array (i.e. the variable $PCLIST), and the -Filter can be adjusted to replace the need for the Where-Object.

    Get-ADComputer -SearchBase 'OU=MyComputerOU, dc=contoso, dc=co, dc=nz' -Filter "name -like 'sales*'" |  
        Select-Object -Expand Name |   
            Sort-Object |  
                Out-File -FilePath TestComputer.txt  
    

    Because you're "new to PowerShell" there are quite a few instructional articles, demonstrations, etc. to e found on the web. But probably the best place to start would be with this book (it's a free PDF download): Windows-PowerShell-4

    Ignore the fact that it's PowerShell version 4. There are only trifling differences between that version and 5.1. Oh, and just ignore the advertisements and praise for Sapien found throughout the book. Sapien's a company that makes good products, but you don't need them now (and you may never need them).

    Get yourself a good editor (VS Code and Virtual Studio are both free) and install the necessary addons to make coding PowerShell a lot easier. You won't regret it.

    1 person found this answer helpful.

  2. Andreas Baumgarten 96,926 Reputation points MVP
    2021-09-20T14:08:21.097+00:00

    Hi @Chris Huang ,

    you can try this (Filter on all lines with Sales):

    $PCList = Get-ADComputer -SearchBase 'OU=MyComputerOU, dc=contoso, dc=co, dc=nz' -Filter '*' |Select -Expand Name  
    $PCList | Where-Object {$_ -match "Sales"} | Out-File -FilePath "TestComputer.txt"  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten