Ok, that means your file is in fact a single column CSV file. Does it have a header value?
IE. Does the top of the file look something like this?
"OU"
"OU=OutsideVendors,OU=Vendors,DC=amer,DC=co,DC=com"
"OU=Users,OU=Vendors,DC=amer,DC=co,DC=com"
Or does it just start out as a list of OUs with no header value?
"OU=OutsideVendors,OU=Vendors,DC=amer,DC=co,DC=com"
"OU=Users,OU=Vendors,DC=amer,DC=co,DC=com"
When you import-csv, the cmdlet returns a collection of PowerShell custom objects as previous discussed. The Column headers are the property names for each object, and each row of data is a different object.
For Example, with the below CSV (OUs.csv):
"OU"
"CN=Users,DC=ca,DC=lab"
"OU=CA,DC=ca,DC=lab"
"CN=Users,DC=ca,DC=lab"
Import-CSV would return 3 objects, each one with a Property called OU
OU
--
CN=Users,DC=ca,DC=lab
OU=CA,DC=ca,DC=lab
CN=Users,DC=ca,DC=lab
When you do a ForEach-Object loop with that collection, $_ represents the current object, but if you want the OU value for that object, you have to specify that property like so. $_.OU
$allOUs = import-csv "C:\Users\Administrator\Desktop\OUs.csv"
$allOUs | ForEach-Object {
$_.OU
}
So if you plug that back into your code, it would look something like this:
$AllOUs |
ForEach-Object {
Get-ADUser -SearchBase $_.OU -Filter * -Properties mailNickname |
Where-Object {$_.mailNickname -notlike "*"} |
select samAccountName,DistinguishedName
}
If your CSV does not have column headers in the first line, then the first OU in the list will be used as the column header, unless you use the -header parameter when you use import-csv.
For Example, with the below CSV (OUs.csv):
"CN=Users,DC=ca,DC=lab"
"OU=CA,DC=ca,DC=lab"
"CN=Users,DC=ca,DC=lab"
Import-CSV would return 3 objects, each one with a Property called OU since that is what you define with the -header parameter
$allOUs = import-csv "C:\Users\Administrator\Desktop\OUs.csv" -Header "OU"
$allOUs | ForEach-Object {
$_.OU
}