# Choose what fields to add to CSV

**URL:** https://forums.powershell.org/t/choose-what-fields-to-add-to-csv/15373
**Category:** PowerShell Help
**Created:** [December 11, 2020, 2:14am UTC](https://forums.powershell.org/t/choose-what-fields-to-add-to-csv/15373 "2020-12-11T02:14:27Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![sharpharp](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@sharpharp](https://forums.powershell.org/u/sharpharp)
#### Post date: [December 11, 2020, 2:14am UTC](https://forums.powershell.org/t/choose-what-fields-to-add-to-csv/15373/1 "2020-12-11T02:14:27Z")

</div>

Hi,

Noob to powershell, been trying for days with no success.

Can anyone help with this snippet, i am trying to output the results to a CSV

&nbsp;

# Load the users

$MailUsers = Get-ADUser -SearchBase “OU=etc etc” -Filter “(SAMAccountName -like ‘A12\*’) -AND `  
(PasswordNeverExpires -eq ‘$false’ -AND Enabled -eq ‘$true’)” -Properties PasswordLastSet, DisplayName, PasswordNeverExpires, mail, SAMAccountName

# Loop through them

foreach ($MailUser in $MailUsers) {

#Write-Output "$($MailUser.SAMAccountName, " “, $MailUser.GivenName)”

# Count how many days are left before the password expires and round that number

$PasswordExpiresInDays = [System.Math]::Round((New-TimeSpan -Start $CurrentPWChangeDateLimit -End ($MailUser.PasswordLastSet)).TotalDays)

# Write some status…

# Write-Output "$($MailUser.SAMAccountName, " “, $MailUser.DisplayName) needs to change password in $PasswordExpiresInDays days.”

# Build the body depending on where in the organisation the user is

if (($PasswordExpiresInDays -eq $LastPasswordWarningDays) -or ($PasswordExpiresInDays -eq $FirstPasswordWarningDays) -or ($PasswordExpiresInDays -eq $SecondPasswordWarningDays) -or ($PasswordExpiresInDays -eq $ThirdPasswordWarningDays)) {

# Write-Output "$($MailUser.SAMAccountName, " “, $MailUser.DisplayName) needs to change password in $PasswordExpiresInDays days.”

}

$MailUser | export-csv $outputFile -append -NoTypeInformation  
}

My Output csv has all the properties in it, I only want the SAMAccountName, DisplayName, Email Address and I also want to Include that Variable $PasswordExpiresInDays in the CSV… The idea is to run this script via SSIS and output to CSV, then use the CSV to send out the emails via another task.

Anyone able to help?

---

<div class="post-metadata">

### Author: ![ralphmwr](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ralphmwr/32/1064_2.png) [@ralphmwr](https://forums.powershell.org/u/ralphmwr)
#### Post date: [December 11, 2020, 2:48am UTC](https://forums.powershell.org/t/choose-what-fields-to-add-to-csv/15373/2 "2020-12-11T02:48:05Z")

</div>

Pipe your objects to Select-Object and pick the properties you want. You can also create new custom properties if needed.

```
$MailUser |
    Select-Object -Property SAMAccountName, DisplayName, EmailAddress,
                            @{n="Password-Expires"; e={[System.Math]::Round((New-TimeSpan -Start $CurrentPWChangeDateLimit -End ($MailUser.PasswordLastSet)).TotalDays)}} |
                Export-Csv $outputFile -Append -NoTypeInformation
```

Recommend reviewing [Select-Object](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-5.1)

---

<div class="post-metadata">

### Author: ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)
#### Post date: [December 11, 2020, 3:58am UTC](https://forums.powershell.org/t/choose-what-fields-to-add-to-csv/15373/3 "2020-12-11T03:58:18Z")

</div>

Couple of things:

- **Splat** - Use splatting rather than the line continuation accent (`) with long commands. It's easy to read and provides the ability to update, add, remove parameters on the fly. Easy to read code and functionality.
- **Date Math** - Not sure what $CurrentPWChangeDateLimit is in your post, but the math should be from when they set the password to Now. This would indicate someone set the password X number of days ago
- **Contains or In** - When you are sending notifications on an interval, this script should be running every day to send notifications to users that meet that interval period. An array of intervals can be used to determine who would get notifications rather than checking if each interval is met with -eq

```
#splatting
$params = @{
    SearchBase = "OU=etc etc" 
    Filter = "(SAMAccountName -like 'A12*') -AND (PasswordNeverExpires -eq '$false' -AND Enabled -eq '$true')" 
    Properties = 'PasswordLastSet', 'DisplayName', 'PasswordNeverExpires', 'mail', 'SAMAccountName'
}

$MailUsers = Get-ADUser @params |
             Select-Object -Property SAMAccountName, 
                                     DisplayName, 
                                     EmailAddress, 
                                     @{Name='PasswordExpiresInDays';Expression={[System.Math]::Round((New-TimeSpan -Start $_.PasswordLastSet -End (Get-Date)).TotalDays)}}

$reminderInterval = 14,10,5,2

foreach ($MailUser in $MailUsers | Where-Object -FilterScript {$_.PasswordExpiresInDays -in $reminderInterval}) {
    #Send-MailMessage ....
}
```

---

<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:28pm UTC](https://forums.powershell.org/t/choose-what-fields-to-add-to-csv/15373/4 "2024-05-16T20:28:10Z")

</div>


