Choose what fields to add to CSV

Welcome Forums General PowerShell Q&A Choose what fields to add to CSV

Viewing 2 reply threads
  • Author
    • #278751
      Topics: 2
      Replies: 0
      Points: 23
      Rank: Member


      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


      # 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?

    • #278760
      Topics: 5
      Replies: 253
      Points: 1,007
      Helping Hand
      Rank: Community Hero

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

      Recommend reviewing Select-Object

    • #278772
      Topics: 17
      Replies: 1951
      Points: 3,988
      Helping Hand
      Rank: Community Hero

      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
Viewing 2 reply threads
  • You must be logged in to reply to this topic.