Correct my script


  • Hello Community,

    I'm pretty much a beginner in the field (shame on me), and I'm not familiar with difficult, more liner PowerShell scripts.

    $distr = Get-Content -Path 'C:\Temp\distributiongroups.csv'
    Foreach ($grp in $distr)
    Get-ADGroup -Identity $grp -Properties * | select -ExpandProperty Mail
    Write-Host $grp
    Get-ADGroupMember -Identity $grp -Recursive | sort name | select -ExpandProperty name

    So here's the script, which produces the following output:

    This is great, that is what I want to achieve. But I cannot put the output what I see as a result in a file (a simple Out-File would be perfect for me). I saw that some people pipelining the Out-File command after the closing curly braces, but it does not work for me and I'm not sure why.

    I appreciate any help!

    Thank you,


    Saturday, November 11, 2017 8:21 AM


All replies

  • # does not work.  Why?
    $p = Get-Process
    foreach($p in $procs){
    } | Out-File names.txt
    # works!  Why?
    Get-Process |

    To understand any advanced technology you must study it.  You cannot guess how to fly a plane by watching planes fly.  You cannot learn PowerSHell by copying what you see.  You must study the basics until you understand.

    Learn PowerShell:


    • Marked as answer by jrvModerator Saturday, November 11, 2017 8:57 PM
    Saturday, November 11, 2017 8:31 AM
  • if you do $p = Get-Process 
    It means that all the process will be in the variable called "p"
    when you do ( $p in $procs)
    Well, the variable is empty so there's nothing in the loop $procs


    Get-process | foreach-object{ #in the previous command
    $_ --> #variable for the current object in powershell
    $_.Name --> #the name property of the current object.

    Then the result goes to the next pipe 
    #(result | =>  command)
    #There is a difference between Foreach-object and foreach
    ($item in $listofItems){ $item}
    #In foreach-object you need to get an list of objects and pipe them
    $ListofObjects | foreach-object{ $_}
    #Case foreach-object (get the object and then use foreach-object (and use the $_ to refer to the current object)
    $p = Get-Process
    $p | ForEach-Object{$_.Name} | Out-File names.txt
    #case foreach (you can't pipe the Out-file
    $x = Get-Process
    foreach($v in $x){$v.Name}  
    #And to get the csv you can't use the pipe in a foreach but you can in a foreach-object
    $x | select Name | out-file names.txt

    • Edited by j0rt3g4 Saturday, November 11, 2017 10:01 AM
    Saturday, November 11, 2017 9:52 AM
  • help about_pipeline

    P.S. - I was hoping the OP would use my questions to do some research.  Simple answers do not allow much room for learning.

    Almost all trained techs can read the docs or do the videos and learn PowerShell over a weekend.  Untrained techs always ask questions and assume they have learned something.  A year later they are still asking the same questions.

    Getting an answer is not the same thing as understanding a technology.


    Saturday, November 11, 2017 3:30 PM
  • Your example and question show you haven't even tried to read the documentation.  This is something a trained tech would always do first.  Without basic knowledge of the technology you will be lost.  You did not even understand the answers that we gave and will not be able to understand them without doing some studying.

    This is not personal PS training forum. It is a forum for techs using PowerShell and having questions about their use of PowerShell. You are asking a question that would be answered in the first two chanters of any book.

    Yes there are some forums for non-techs to ask simple questions.  If you are not interested in formally learning PowerShell then maybe one of those forums would be more suitable.  If you really want to learn PowerShell then review the following.

    With a little effort you can learn PowerShell.

    Please read this first:

    Also find scripts here:

    Learn PowerShell:

    Script requests:


    Saturday, November 11, 2017 8:23 PM
  • I came here because lack of knowledge in this field, and tried to get a little push which may help me to do some research and widen my knowledge.

    Unfortunately instead of helping you started to insult me / my knowledge unnecessairly. This is very unprofiessional conduct, because a forum is for helping people who need a little help. If everybody would know everything, there would be no meaning of a forum.

    I still fail to see where I have insulted you.  Perhaps you consider accurate observations about you level of skill an insult.  We have to start somewhere.  Not knowing something is only a handicap if you refuse to admit your lack of knowledge.

    The way I find that I have to learn something that is very difficult is to erase my head and start over by assuming I know nothing about the subject.  I then read, study and test until I get a clear idea of what I am doing.

    Never get what you think you know get in the way of learning something new.  Most GUI users and support people have a very limited view of computers because the GUI removes all of the underlying technology.  The GUI is designed to allow minimally trained people to use computers without having to learn computer or software engineering.  Programming (scripting) require that you learn more and learn new ways of learning things.   Of course PowerShell also allows you  to just type simple commands like "dir" and "type" or other more modern commands like "Get-Process". Beyond that you will have to learn about programming and how modern systems based on the Bet Framework are designed and used.  You will also have to learn about object systems and OOP (Object Oriented Programming).  This can all be learned well enough to use PowerShell to do complex things but it takes some basic studying.

    Of course you can just read blogs and forums but how will you know what is accurate and what is just bad information sending you down the rabbit hole?


    Saturday, November 11, 2017 9:09 PM
  • They JRV this is can't be the answer to the question. This is a suggestion not an answer.
    Saturday, November 11, 2017 9:39 PM
  • The fifth post from the top.  The first line is the answer.

    The post marked as an answer was  simple answer.

    Your post, j0rt3g4, is an answer. 

    My point is that all of those answers are of minimal help to anyone who has no foundation in the technology.

    You can hang around an airport and ask questions.  It will not teach you how to fly a 767.


    Saturday, November 11, 2017 9:57 PM
  • Whatever you are god and lord of this forum for your score.
    Sunday, November 12, 2017 5:56 AM
  • I digged myself in a little bit more and find out what was the problem of my thinking. Thank you for the suggestions!

    So here's the corrected script:

    $distr = Get-Content -Path 'C:\Temp\distributiongroups.csv'
    Foreach ($grp in $distr)
    Get-ADGroup -Identity $grp -Properties * | select -ExpandProperty Mail
    Get-ADGroupMember -Identity $grp -Recursive | sort name | select -ExpandProperty name

    Then ran the script from the cmdline and piped it into a file.


    Monday, November 13, 2017 9:13 AM
  • Hi,

    Good to hear that you have solved this issue by yourself. In addition, thanks for sharing your solution in the forum as it would be helpful to anyone who encounters similar issues.

    If there is anything else we can do for you, please feel free to post in the forum.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact

    Monday, November 13, 2017 9:24 AM