-1

Im basically comparing an short list of PC names with our active directory, the first file is the list of pcs i have to check, the second file is the ad list that i got with following command

$ADcomputer = get-adcomputer -Filter "name -Like '*'" -Searchbase "DC=**,DC=******,DC=**"

($ADcomputer).name | Out-file "C:\temp\ADComputer.csv"

In both lists there are only pc names in split "A" most of them are built like "PC23425" just with various numbers (there are some exceptions of course but i dont need to check for them only for pcs with this pattern).

The list.csv is built like this

PC020682
PC020891
PC020953
PC021999

I read several posts about this and im very sure i need to use a foreach loop for that, but since i dont use powershell very often i aint sure how to pull it off.

What i try is

$Path = "C:\temp"
$ComputerList = Import-Csv -Path "$($path)\ADcomputer.csv"
$ComputerAchiv = Import-Csv -Path "$($path)\List.csv"
   

But i have no idea how to build the foreach loop.

Theo
  • 57,719
  • 8
  • 24
  • 41
Trippin
  • 73
  • 1
  • 8
  • 6
    This line `($ADcomputer).name | Out-file ...` creates not a CSV file, but a text file instead where all pc names are listed each on a separate line. Therefore there is no `"Row A"`. How is the second file created?? Is that not a CSV file as well? Open your `List.csv` file in notepad and copy the first 3 or 4 lines. Then [edit](https://stackoverflow.com/posts/69617533/edit) your question and paste that in there. – Theo Oct 18 '21 at 14:08
  • @Theo yes the second file is also CSV, but i dont know how it was created since it was only send to me. I tried ```Export-CSV``` for the AD list but like that i only got the sting lenghts from the names and not the names. – Trippin Oct 18 '21 at 14:10
  • 1
    `Get-Content "$($path)\List.csv" |Where-Object { $ADcomputer.name -eq $_ }` – iRon Oct 18 '21 at 14:14
  • @iRon thank you very much, thats exacly what i needed, also with out an foreach loop which just would took longer. – Trippin Oct 18 '21 at 14:17
  • 2
    Glad iRon could help you, but you should really look into what makes a CSV file a structured CSV file and what the difference is between such a file and a simple text file. This could help you decide whether to use `Import-Csv` or `Get-Content` – Theo Oct 18 '21 at 14:23
  • @iRon Please post as answer. – Theo Oct 18 '21 at 14:24
  • 2
    As noted in the initial comment: If a file contains a list of names but doesn't contain an initial line containing a property name, then **it is not a CSV file**. It's just a text file. As noted in another comment, if (or because, in this case) you do not have a CSV file, then you would use `Get-Content` rather than `Import-Csv` (because again, it is not a CSV file but rather just a plain text file). – Bill_Stewart Oct 18 '21 at 14:33

1 Answers1

1

Although a CSV file format is not fully standardized, as mentioned by @Theo PowerShell expects the first row to be the header record by default. The header row will be used to define the property names of the object(s) eventually emitted by the Import-Csv cmdlet. In the case your CSV file doesn't contain a header row, you might manually add the header row (column names, property names) by using the -Header parameter, e.g.: -Header username.
For PowerShell you files are simple lists (without properties), which you might read using the Get-Content cmdlet:

$ComputerList = Get-Content -Path "$($path)\ADcomputer.csv"
$ComputerAchiv = Get-Content -Path "$($path)\List.csv"

To find out the items that the lists share, you might use the Where-Object cmdlet:
(see also: Comparing two arrays & get the values which are not common)

$ComputerList |Where-Object { $ADcomputer.name -eq $_ }

Next Level

Although this might be a direct answer to your question. It might not bring you much as you probably do want to deal with PowerShell standard CSV files (Meaning PowerShell object lists) and join those. For this, see e.g.: In Powershell, what's the best way to join two tables into one?

iRon
  • 20,463
  • 10
  • 53
  • 79