locked
import csv along with foreach and get-aduser showing no output RRS feed

  • 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 


    \_(ツ)_/


    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''






    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.


    \_(ツ)_/

    I fixed it multiple times....that's what happens when you rush :-)

    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