export seperate csv files

Welcome Forums General PowerShell Q&A export seperate csv files

This topic contains 4 replies, has 2 voices, and was last updated by

 
Participant
1 month ago.

  • Author
    Posts
  • #111953

    Participant
    Points: 0
    Rank: Member

    Hello,

    I am still learning all of the ins and outs of PowerShell so if this is an easy answer I apologize for not knowing it.  My task is to create a separate csv file contacting the email contact information from specific Distribution Groups.

    I know how to export the list to a single file one a time taking the input from the screen and exporting the information to a pre-named csv file  I have used the append command to place all the information in one file and then break out the information in Excel.  Or by adding the group name to the export command doing one group at a time.  What I cannot figure out is how to create the each individual csv file based on what I key in at the screen.  I keep getting different errors.

    What I would like to eventually get to is read in a csv file that has the list of Distribution Groups that I need the information from.  I would like to export each group into its own file.  I realize that I probably need to create a loop that retrieves the distribution group name from a file on at a time.  But since I cannot get the file created by name from what I key in, I cannot export each group in its own named file.

    Here is what I have:

    
    
    
    
    Normal
    0
    
    false
    false
    false
    
    EN-US
    X-NONE
    X-NONE
    
    
    
    
    
    /* Style Definitions */
    table.MsoNormalTable
    {mso-style-name:"Table Normal";
    mso-tstyle-rowband-size:0;
    mso-tstyle-colband-size:0;
    mso-style-noshow:yes;
    mso-style-priority:99;
    mso-style-parent:"";
    mso-padding-alt:0in 5.4pt 0in 5.4pt;
    mso-para-margin-top:0in;
    mso-para-margin-right:0in;
    mso-para-margin-bottom:10.0pt;
    mso-para-margin-left:0in;
    mso-pagination:widow-orphan;
    font-size:12.0pt;
    mso-bidi-font-size:11.0pt;
    font-family:"Calibri","sans-serif";
    mso-ascii-font-family:Calibri;
    mso-ascii-theme-font:minor-latin;
    mso-hansi-font-family:Calibri;
    mso-hansi-theme-font:minor-latin;}
    
    $GroupName=Read-Host "Enter the name of Group for Which you want to Export Contacts"
    $memberof=get-adgroup $GroupName |select -expandproperty distinguishedname
    Get-ADObject -Filter 'objectclass -eq "contact" -and memberof -eq $memberof' -properties *|select name,mail,@{e={"$($_.memberof)"};l="Member Of"} | Export-CSV c:\temp\DL Owners\test.csv -notype -Encoding UTF8
    
  • #111959

    Participant
    Points: 0
    Rank: Member

    What I cannot figure out is how to create the each individual csv file based on what I key in at the screen.

    Use the variable in the name of the file.  Note how quotes are used around the path to expand the variable $GroupName.

    $GroupName=Read-Host "Enter the name of Group for Which you want to Export Contacts"
    $memberof=get-adgroup $GroupName |select -expandproperty distinguishedname
    Get-ADObject -Filter 'objectclass -eq "contact" -and memberof -eq $memberof' -properties *|select name,mail,@{e={"$($_.memberof)"};l="Member Of"} | Export-CSV "c:\temp\DL Owners\$GroupName.csv" -notype -Encoding UTF8
    
    

    To use an input file, you're right that  you'll need a loop for this.  Specifically you want to be looking at a foreach loop.

    $myList = Get-Content c:\temp\list.txt
    
    foreach ($item in $myList) {
    
    Do_Something | Export-CSV "$item.csv"
    
    }
    
    

    I've kept the above pretty vague to give you a chance to explore the help (Get-Help about_foreach) for yourself but do reply if you need some clarification.

    Obligatory mention for PowerShell in a Month of Lunches, it will teach you all of this and much much more.

     

    • #111961

      Participant
      Points: 0
      Rank: Member

      Hi Matt,

      I did try using the $GroupName.csv (which i should have mentioned & with the quotes) only to have the file created as $GroupName.csv with all of the information.  I did search and try different things before coming here.

      I read the about the Month of Lunches and I will try it out.

      Any other suggestions?  I will try the loop after I get the first part to work.

      Thank you for your help.

  • #111965

    Participant
    Points: 0
    Rank: Member

    Quotes around the path should work fine.  Did you definitely have double quotes and not single quotes?

    Although you can often use both interchangeably, without any problem, there is an important difference: single quotes define a string literal i.e. it will appear/be processed exactly as you type it.  Double quotes cause any variable names to be replaced with the variable value.

     

     

  • #111967

    Participant
    Points: 0
    Rank: Member

    That is exactly what i did wrong!  Single and not double because i did not know any better.  I figured it had to be something simple that i just did not know.  I will next work on the loop.

    Thank you.

You must be logged in to reply to this topic.