header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Export-CSV append
Last Post 20 Nov 2008 08:16 PM by jhames. 2 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Resolved
jhamesUser is Offline
New Member
New Member
Posts:4
Avatar

--
20 Nov 2008 06:28 PM  

I have an interesting problem that I am trying to solve. We have recently sold a portion of our company to another company and I need to export the AD and mail information about their accounts into a spreadsheet. I am going to use this information to create a set of contacts that forward any email sent to their old address to their new address. Below is what I have been able to do so far. I get a collection of objects with the get-recipient and pipe it to a forEach. Inside of the ForEach I get the fields that I need. I have to get the email addresses separately because they will not resolve in the export-csv in less I convert them to text. If I have the export-csv line inside of the ForEach it will overwrite the previous record. If I have it on the outside I don't get anything because of the scope of the $mbdata variable is local to the ForEach. I don't want to use out-file because it would mess up the formatting. Does anyone have any thoughts on how I should go with this script?

get-recipient -filter "displayname -like '*(Company name)'" -resultsize 3000 | `

ForEach-Object {

$mbdata = $_| select Name,alias,company,customattribute5,externalemailaddress,displayname, `

firstname,organizationalunit,phone,primarysmtpaddress,recipienttype,distinguishedname, `

identity,database,emailaddresses

$mbdata.emailaddresses = [string]::join("`t", ($mbData.EmailAddresses) )

Export-Csv .\test.csv -inputobject $mbdata -noTypeInformation

}

'done!'

PoshoholicUser is Offline
PowerShell MVP
New Member
New Member
Posts:38
Avatar

--
20 Nov 2008 06:52 PM  
You can do what you need in a one-liner. Well, everything will be on one line except the output of 'done!' at the end. Here's the script:

get-recipient -filter "displayname -like '*(Company name)'" -resultsize 3000 `
| select Name,alias,company,customattribute5,externalemailaddress,displayname, `
firstname,organizationalunit,phone,primarysmtpaddress,recipienttype,distinguishedname, `
identity,database,@{Name='emailaddresses';Expression={[string]::join("`t", ($_.EmailAddresses))}} `
| Export-Csv .\test.csv -noTypeInformation
'done!'

This takes your recipients, selects a bunch of properties, calculates one property, and exports the result object to a csv file without the type information.

--
Kirk Munro [MVP]
Poshoholic
http://poshoholic.com
jhamesUser is Offline
New Member
New Member
Posts:4
Avatar

--
20 Nov 2008 08:16 PM  
Thanks! Kirk this works perfectly.
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer