question

ChrisHuang-6081 avatar image
0 Votes"
ChrisHuang-6081 asked RichMatheisen-8856 commented

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

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered ChrisHuang-6081 commented

Hi @ChrisHuang-6081 ,

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for your help Andreas. Much appreciated. I will give it a test and let you know if there is any issues. Thank you.

0 Votes 0 ·
RichMatheisen-8856 avatar image
1 Vote"
RichMatheisen-8856 answered RichMatheisen-8856 commented

A slight modification of the example posted by @AndreasBaumgarten.

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.



· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Rich,

Thank you for your response and sending the link to a free eBook. I have already downloaded it and will start reading it soon. Thank you very much.

Just a question around the example code you provided above, If I also would like to output hostname that matches with multiple keyword e.g. Sales_PC1, HR_PC1, OPS_PC1....

Am I able use something like, basically there are thousands of computer objects in MyComputerOU. I am keen to output a list of hostnames that matches with a few different keywords.

                       Get-ADComputer -SearchBase 'OU=MyComputerOU, dc=contoso, dc=co, dc=nz' -Filter "name -like 'sales*' OR 'IT*' OR 'OPS*'"


Thanks again for your help.

Thanks,
Chris

0 Votes 0 ·

Yes, you can have multiple conditions in the -Filter string. But each condition must be complete. The operators are not transitive.

 Get-ADComputer -SearchBase 'OU=MyComputerOU, dc=contoso, dc=co, dc=nz' -Filter "name -like 'sales*' -OR name -like 'IT*' -OR name -like 'OPS*'"
0 Votes 0 ·