# Exchange PS and foreach

**URL:** <https://forums.powershell.org/t/exchange-ps-and-foreach/7545>\
**Category:** PowerShell Help\
**Created:** [November 17, 2016, 8:46am UTC](https://forums.powershell.org/t/exchange-ps-and-foreach/7545 "2016-11-17T08:46:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jan-kare-eiken-reils](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jan-kare-eiken-reils](https://forums.powershell.org/u/jan-kare-eiken-reils)\
**Post date:** [November 17, 2016, 8:46am UTC](https://forums.powershell.org/t/exchange-ps-and-foreach/7545/1 "2016-11-17T08:46:02Z")

</div>

Hi,

I am really frustrated.

I am trying to complete a script that give me all inactive users in AD and their current mailbox sizes in Exchange based on samaccountname.

what i got :

$Days=180  
$OU=“Customer OU’s”  
$OutputfilePath = “c:\Report\180daysincelogon\_”+([datetime]::Now).tostring(“yyyy-MMM-dd”)+“.csv”

get-aduser -filter \* -searchscope subtree -searchbase $OU -properties emailaddress,DisplayName,lastlogontimestamp,whencreated | ? {(((Get-date) - ([datetime]::FromFileTime($_.lastlogontimestamp))).TotalDays -gt $days)} | select Emailaddress,DisplayName,samaccountname,Userprincipalname,whencreated, @{Exp={([datetime]::FromFileTime($_.lastlogontimestamp))};label=“Last logon time stamp”} | Sort-Object “Last logon time stamp” | export-csv $OutputfilePath -NoTypeInformation

$csv = Import-Csv $OutputfilePath  
foreach ($line in $csv)  
{  
(get-mailboxstatistics -identity $line.samaccountname | select Totalitemsize,itemcount | export-csv $OutputfilePath -Force -Append)  
}

I suppose this could be done in a much different and better way, but I am still learning 🙂

What i need is to add header in csv file before the import-csv cmdlet. I know i need to add it as a property but is not able to do it.

Is there any tips & tricks ?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 17, 2016, 9:15am UTC](https://forums.powershell.org/t/exchange-ps-and-foreach/7545/2 "2016-11-17T09:15:13Z")

</div>

(untested)

If you want to have all information in one csv you have to export it in one chunk … maybe like this:

```
$Days=180
$OU=“Customer OU’s”
$OutputfilePath = “c:\Report\180daysincelogon_”+([datetime]::Now).tostring(“yyyy-MMM-dd”)+“.csv”

$ADUSers = get-aduser -filter \* -searchscope subtree -searchbase $OU -properties emailaddress,DisplayName,lastlogontimestamp,whencreated | ? {(((Get-date) – ([datetime]::FromFileTime($\_.lastlogontimestamp))).TotalDays -gt $days)}  
foreach ($ADUser in $ADUSers) {  
$MBStats = get-mailboxstatistics -identity $ADUSer.samaccountname | select Totalitemsize,itemcount  
$OutputObj = [PSCustomObject]@{  
Emailaddress = $ADUser.Emailaddress  
DisplayName = $ADUser.DisplayName  
samaccountname = $ADUser.samaccountname  
Userprincipalname = $ADUser.Userprincipalname  
whencreated = $ADUser.whencreated  
LastLogonTimeStamp = ([datetime]::FromFileTime($ADUser.lastlogontimestamp))  
Totalitemsize = $MBStats.Totalitemsize  
itemcount = $MBStats.itemcount  
}  
}

$OutputObj

$OutputObj | export-csv $OutputfilePath -Force -NoTypeInformation
```

---

<div class="post-metadata">

**Author:** ![matt-mcnabb](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-mcnabb](https://forums.powershell.org/u/matt-mcnabb)\
**Post date:** [November 17, 2016, 9:39am UTC](https://forums.powershell.org/t/exchange-ps-and-foreach/7545/3 "2016-11-17T09:39:46Z")

</div>

Olaf:

The example above will only output one object, rather than the entire array, because OutputObj is being assigned inside the foreach loop. You should be able to fix this by moving the assignment directly before the foreach keyword:

```
$Days=180
$OU="Customer OU's"
$OutputfilePath = "c:\Report\180daysincelogon_"+([datetime]::Now).tostring("yyyy-MMM-dd")+".csv"

$ADUSers = get-aduser -filter * -searchscope subtree -searchbase $OU -properties emailaddress,DisplayName,lastlogontimestamp,whencreated | ? {(((Get-date) – ([datetime]::FromFileTime($_.lastlogontimestamp))).TotalDays -gt $days)} 
$OutputObj = foreach ($ADUser in $ADUSers) {
    $MBStats = get-mailboxstatistics -identity $ADUSer.samaccountname | select Totalitemsize,itemcount
    [PSCustomObject]@{
        Emailaddress = $ADUser.Emailaddress
        DisplayName = $ADUser.DisplayName
        samaccountname = $ADUser.samaccountname
        Userprincipalname = $ADUser.Userprincipalname
        whencreated = $ADUser.whencreated
        LastLogonTimeStamp = ([datetime]::FromFileTime($ADUser.lastlogontimestamp))
        Totalitemsize = $MBStats.Totalitemsize
        itemcount = $MBStats.itemcount
    }
}

$OutputObj

$OutputObj | export-csv $OutputfilePath -Force -NoTypeInformation
```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 17, 2016, 10:02am UTC](https://forums.powershell.org/t/exchange-ps-and-foreach/7545/4 "2016-11-17T10:02:00Z")

</div>

Oops … you’re right. Thanks for the correction.

---

<div class="post-metadata">

**Author:** ![jan-kare-eiken-reils](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jan-kare-eiken-reils](https://forums.powershell.org/u/jan-kare-eiken-reils)\
**Post date:** [November 23, 2016, 4:34am UTC](https://forums.powershell.org/t/exchange-ps-and-foreach/7545/5 "2016-11-23T04:34:40Z")

</div>

Thanks for the response 🙂

When running without the $outputObj in front of the foreach loop i get one line written in the csv. When putting the $OutputObj in front the csv is empty.

Seems the loop isnt looping 🙂

Thanks for replies 🙂

---

<div class="post-metadata">

**Author:** ![jan-kare-eiken-reils](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jan-kare-eiken-reils](https://forums.powershell.org/u/jan-kare-eiken-reils)\
**Post date:** [November 23, 2016, 5:36am UTC](https://forums.powershell.org/t/exchange-ps-and-foreach/7545/6 "2016-11-23T05:36:41Z")

</div>

Nevermind 🙂 Figured it out. Had mistyped 🙂

Thanks for the help !!!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:39pm UTC](https://forums.powershell.org/t/exchange-ps-and-foreach/7545/7 "2024-05-16T20:39:51Z")

</div>

Closed on May 16, 2024, 8:39pm UTC


