Jump to content

how to format this powershell output correctly


Recommended Posts

Posted

It's not having an error but it's coming out wrong.

 

>import-csv "D:\allemail.csv" | foreach {get-mailboxstatistics -identity $_.userprincipalname | select $_.displayname,lastuseractiontime }

 

allemail.csv contains a column of emailaddress and a column of names with the headings userprincipalname and displayname

 

At the moment the heading of the output is like this:

 

Pastoral Triage LastUserActionTime

--------------- ------------------

04/04/2019 12:18:20

22/11/2017 15:57:38

13/11/2018 13:54:15

01/02/2018 2

 

if I replace $_.displayname with identity I get which is no use.

 

Identity LastUserActionTime

-------- ------------------

943d8acf-a411-429c-99af-4fd77add3111 04/04/2019 12:18:20

a3a85b4c-a751-4ea4-bbd9-584bc7c38111 22/11/2017 15:57:38

 

 

It's probably something simple but I haven't found a example that's similar enough to get the answer from.

Posted
What do you want the output to be?

 

ideally userprincipalname and lastuseractiontime.

 

 

so i can find out what emails haven't logged on for ages.

Posted
It's probably something simple but I haven't found a example that's similar enough to get the answer from.

Would this do what you need?

 

$Mailboxes = Get-Mailbox -Filter {(RecipientTypeDetails -eq "UserMailbox") -or (RecipientTypeDetails -eq "SharedMailbox")} –ResultSize Unlimited -ErrorAction SilentlyContinue | Sort
$Mailboxes | ForEach { Get-MailboxStatistics -Identity $_.UserPrincipalName | Select DisplayName, Last* } | Export-Csv -Path D:\Stats.csv -NoTypeInformation

  • Thanks 1
Posted

You don't need to specify the current pipeline object with "$_.Displayname" as you are within a foreach loop running through UserPrincipalNames. Just use "DisplayName, LastUserActionTime"

 

[color=#333333]import-csv "D:\allemail.csv" | foreach {get-mailboxstatistics -identity $_.userprincipalname | select displayname,lastuseractiontime }
[/color]

  • Thanks 1
Posted
You don't need to specify the current pipeline object with "$_.Displayname" as you are within a foreach loop running through UserPrincipalNames. Just use "DisplayName, LastUserActionTime"

 

[color=#333333]import-csv "D:\allemail.csv" | foreach {get-mailboxstatistics -identity $_.userprincipalname | select displayname,lastuseractiontime }
[/color]

 

 

That's it. Thanks!

Posted
You don't need to specify the current pipeline object with "$_.Displayname" as you are within a foreach loop running through UserPrincipalNames. Just use "DisplayName, LastUserActionTime"

 

[color=#333333]import-csv "D:\allemail.csv" | foreach {get-mailboxstatistics -identity $_.userprincipalname | select displayname,lastuseractiontime }
[/color]

 

 

on reflection i'd prefer to have userprincipalname,displayname, lastuseractiontime as the output but that doesn't work the same way. I get a blank column if I just do it the same.

Posted

This is a bit dirty but it will do what you need. Normally I would go the PSObject route.

 

import-csv "D:\allemail.csv" | foreach {$UPN = $_.UserPrincipalName; get-mailboxstatistics -identity $UPN | select @{n='UserPrincipalName';e={$UPN}},displayname,lastuseractiontime}

  • Thanks 1
Posted
This is a bit dirty but it will do what you need. Normally I would go the PSObject route.

 

import-csv "D:\allemail.csv" | foreach {$UPN = $_.UserPrincipalName; get-mailboxstatistics -identity $UPN | select @{n='UserPrincipalName';e={$UPN}},displayname,lastuseractiontime}

 

 

Thanks. easy when you know how :)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...