Solved

Sort CSV by Column Name in Powershell

Posted on 2013-12-20
2
2,185 Views
Last Modified: 2013-12-20
I'm using the below script, however would like the results sorted Ascending by Mailbox name.  Where do I put the Sort command in the script for this? I tried putting Sort-Object Mailbox at the end, but it did not work.  Thanks in advance.

$Mailboxes = Get-Mailbox -Filter {RecipientTypeDetails -eq "UserMailbox"} -ResultSize Unlimited
$Output = @()
ForEach ($Mailbox in $Mailboxes) {
 $Calendar = (($Mailbox.PrimarySmtpAddress.ToString())+ ":\" + (Get-MailboxFolderStatistics -Identity $Mailbox.DistinguishedName -FolderScope Calendar | Select-Object -First 1).Name)
 $Permissions = Get-MailboxFolderPermission -Identity $Calendar
 ForEach ($Permission in $Permissions) {
  $Permission | Add-Member -MemberType NoteProperty -Name "Mailbox" -value $Mailbox.DisplayName
  $Output = $Output + $Permission
 }
}
$Output | Select-Object Mailbox, User, {$_.AccessRights}, IsValid | Export-Csv -Path C:\CalendarPermissions.csv -NoTypeInformation | Sort-Object Mailbox

Open in new window

0
Comment
Question by:fireguy1125
2 Comments
 
LVL 13

Accepted Solution

by:
imkottees earned 400 total points
ID: 39731873
Hi,

always sort should come before selecting/formatting the output.

Try like this.

$Output | Sort-Object Mailbox | Select-Object Mailbox, User, {$_.AccessRights}, IsValid | Export-Csv -Path C:\CalendarPermissions.csv -NoTypeInformation

http://blogs.technet.com/b/heyscriptingguy/archive/2012/01/10/order-your-output-by-easily-sorting-objects-in-powershell.aspx
0
 
LVL 39

Assisted Solution

by:footech
footech earned 100 total points
ID: 39732104
I would typically put it after the Select-Object command, but it will work in front of it as well.  This can be important when you are using Select-Object to create calculated properties, and you then want to sort by that calculated property.  But yes, it has to come before formatting commands.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now