Asked by:
import csv along with foreach and get-aduser showing no output

Question
-
Hi Team,
I know there are many blogs related to my question along with discussions but after going through all those, I am not able to findout the good straight forward answer. Moreover i am also facing the same issue with my Windows 7 Powershell.
When I am typing, the below command I am getting nothing in output without any error too:-
import .\name.csv | foreach {get-aduser -filter {userprincipalname -like '$_.name'} | select name, sAMAccountName}
Even i tried with below after going through another article but no success:
import .\name.csv | foreach {get-aduser -filter {userprincipalname -like '$($_.name)'} | select name, sAMAccountName
Does any one know where I am lagging on above commands or is this the issue with Powershell module itself.
My CSV file just for information contains below:-
Name
johny@xcm.com
abc@xcm.com
bbb@xcm.com
Thanks in advance.
Tuesday, March 6, 2018 1:27 PM
All replies
-
Import-Csv .\name.csv | ForEach-Object{ Get-AdUser -filter "userprincipalname -eq '$($_.name)'" } | Select-Object name, sAMAccountName
\_(ツ)_/
- Edited by jrv Tuesday, March 6, 2018 1:34 PM
- Proposed as answer by Albert LingMicrosoft contingent staff Wednesday, March 7, 2018 4:20 AM
Tuesday, March 6, 2018 1:34 PM -
import-csv .\name.csv | foreach { get-aduser -filter "userprincipalname -eq '$($_.name)'"} | select name, sAMAccountName
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''
- Edited by clayman2 Tuesday, March 6, 2018 2:09 PM typo
- Proposed as answer by Albert LingMicrosoft contingent staff Wednesday, March 7, 2018 4:20 AM
Tuesday, March 6, 2018 1:34 PM -
import-csv .\name.csv | foreach { get-aduser -filter "userprincipalname -like '$($_.name)'"} | select name, sAMAccountName
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''
It is in a string so it won't expand.
\_(ツ)_/
Tuesday, March 6, 2018 1:53 PM -
Don't use "like" when you are trying to match a single item without using a wild card. This creates confusion and will fail in many scenarios.
You are looking for an exact match to the UPN string. All variables must be expanded before passing to the filter so the whole thing must be in double quotes. The internal single quotes are required and the dot noted property must be executed as a subexpression.
\_(ツ)_/
Tuesday, March 6, 2018 1:57 PM -
import-csv .\name.csv | foreach { get-aduser -filter "userprincipalname -like '$($_.name)'"} | select name, sAMAccountName
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''
It is in a string so it won't expand.
\_(ツ)_/
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''
Tuesday, March 6, 2018 2:10 PM