0

I had a look at other questions and couldn't find one which helped me with my particular issue, apologies

I have 100+ items in a CSV and I want to put each into two sections of a command. I want this to then be looped through using the following:

$Users = Import-Csv -Path "C:\name\test.csv"       
foreach ($user in $users)
{
    {

       $export = Get-DistributionGroupMember "$user" | Select-Object Identity,Alias,City,Company  | Export-Csv "$user.csv" 
    }
}

What am i looking over?

edit

This is the current output this is the current output, i'm hoping that the $user would be replaced by each line in the CSV

14
  • Where are you defining/setting values for $csv and $title? Commented Feb 6, 2019 at 12:53
  • @JeffZeitlin - i've updated the code, apologies had copied one I was playing around with Commented Feb 6, 2019 at 12:55
  • There's no need for the double {{ }} only one pair is needed with foreach. So what exactly is your problem/question, "looking over" doesn't mean much? Commented Feb 6, 2019 at 12:56
  • What results are you getting, and what results do you expect to get? Commented Feb 6, 2019 at 12:57
  • @JeffZeitlin Apologies again, added Commented Feb 6, 2019 at 12:58

2 Answers 2

3

The problem is the double {{ }} you have with the foreach:

Here's a quick demo to show what it's doing:

PS C:\WINDOWS\system32> 1..3 | foreach {Write-Host $_}
1
2
3

PS C:\WINDOWS\system32> 1..3 | foreach {{Write-Host $_}}
Write-Host $_
Write-Host $_
Write-Host $_
Sign up to request clarification or add additional context in comments.

3 Comments

Hmmm... I guess that it does make a difference; I wonder why? I'd gotten the impression that {} was like the same in C/C++/C#, or like BEGIN...END in Pascal.
@JeffZeitlin The nested curly brackets define a scriptblock (basically an anonymous function), which doesn't magically invoke itself. What's a bit confusing about PowerShell is that in some cases the curly brackets define begin/end of a nested context (e.g. in foreach loops or try..catch statements) while in other cases they define scriptblocks that are passed as arguments (e.g. with ForEach-Object or Where-Object).
@James C - I've done all changes suggested and now seem to have a working script my main issue is that instead of just using the text in the CSV it's adding "@{alias=<text>}" instead of just <text> - any ideas?
0

The exact code which resolved the issue:

$Users = Import-Csv -Path "C:\name\test.csv"       
foreach ($user in $users)
{ 

       $export = Get-DistributionGroupMember $user.Alias | Select-Object Identity,Alias,City,Company  | Export-Csv "$User.csv"
}

Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.