locked
How do I Export multiple line output to a TXT or CSV file? RRS feed

  • Question

  • Hello,

    This script generates a list of users, then lists their name, GivenName, SurName, then lists their Group Memberships beneath that with an inserted space.  Its exactly what I want ont the screen, but how do I generate to either a TXT or CSV file?

    Thanks in advance.

    $Users = get-aduser -filter 'SamAccountName -like "User-*"'
    Foreach ($User in $Users) 
    { 
    Write-Host $user.name $user.GivenName$user.SurName 
    Get-ADPrincipalGroupMembership $User | foreach-object { $_.Name} 
    echo `n 
    }


    Thanks for your help! SdeDot

    Thursday, September 27, 2012 12:58 AM

Answers

  • Getting output text to a file is relatively easy.  Here's one way, leveraging your existing  code:

    $Users = get-aduser -filter 'SamAccountName -like "User-*"'
    &{Foreach ($User in $Users){
         '{0} {1} {2}' -f $user.name,$user.GivenName,$user.SurName 
         Get-ADPrincipalGroupMembership $User | foreach-object { $_.Name} 
         "`n" 
         }
      } | set-content output.txt
    CSV will require a little more work, since you've got one multi-valued attribute (group membership) you'll first need to decide how that would need to be represented in a columnar data format. 

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by SdeDot Thursday, September 27, 2012 2:24 AM
    Thursday, September 27, 2012 1:08 AM

All replies

  • You can't do that with a proper .csv file.

    Try this for .txt file...

    $Users = get-aduser -filter 'SamAccountName -like "User-*"'
    (Foreach ($User in $Users)
    {
    Write-Host $user.name $user.GivenName$user.SurName
    Get-ADPrincipalGroupMembership $User | foreach-object { $_.Name}
    echo
    `n
    }) | Out-String | Set-Content 'c:\mytextfile.txt'


    Admiral Ackbar says...

    Thursday, September 27, 2012 1:04 AM
  • Getting output text to a file is relatively easy.  Here's one way, leveraging your existing  code:

    $Users = get-aduser -filter 'SamAccountName -like "User-*"'
    &{Foreach ($User in $Users){
         '{0} {1} {2}' -f $user.name,$user.GivenName,$user.SurName 
         Get-ADPrincipalGroupMembership $User | foreach-object { $_.Name} 
         "`n" 
         }
      } | set-content output.txt
    CSV will require a little more work, since you've got one multi-valued attribute (group membership) you'll first need to decide how that would need to be represented in a columnar data format. 

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by SdeDot Thursday, September 27, 2012 2:24 AM
    Thursday, September 27, 2012 1:08 AM
  • Riffy:  I get an error in your code in the (Foreach ($User in $Users) line:

    + (Foreach ($User in <<<<  $Users) 
        + CategoryInfo          : ParserError: (in:String) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : UnexpectedToken

    Mjolinor:  Your code worked fine!  As you would expect, this output is for auditors, so they will want it in CSV format so they can filter it, etc.  Not sure I understand your comment about 'represented in a columnar data format'  In any case, thanks for the help here.


    Thanks for your help! SdeDot

    Thursday, September 27, 2012 1:15 AM
  • Not sure I understand your comment about 'represented in a columnar data format'  In any case, thanks for the help here.

    Just imagine you were going to put it into an Excel spreadsheet.

    It's easy to have a column for Name, GivenName, and SurName, and then a row for each user, but now where does that list of group memberships fit?  Do you cram all of them together into the next cell, or do you duplicate the user name fields once for each group membership so that each group name gets it's own cell?


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, September 27, 2012 1:47 AM
  • Ahhh... now it makes sense given the way you have explained it.  Thank you!

    Thanks for your help! SdeDot

    Thursday, September 27, 2012 2:24 AM