Script Repository


Export user properties to CSV file

May 20, 2021
215

The script exports specified properties of the target user to a CSV file and sends the file via email. To execute the script, create a scheduled task, a custom command or a business rule configured for the User object type.

Parameters:

  • $csvFilePath - Specifies the path to the CSV file to be created.
  • $removeCsvFile - Specifies whether the CSV file will be removed after sending the email notification.
  • $propertiesToExport - Specifies the LDAP names of the properties whose values will be present in the CSV file.
  • $to - Specifies the recipient email address.
  • $subject - Specifies the email notification subject.
  • $from - Specifies the email address from which the email notification will be sent.
  • $mailServer - Specifies the FQDN of the mail server that will be used to deliver the email notification.
  • $body - Specifies the email notification body.
Edit Remove
PowerShell
$csvFilePath = "C:\Scripts\Report.csv" # TODO: modify me
$removeCsvFile = $True # TODO: modify me
$propertiesToExport = @("givenName", "middleName", "sn", "title", "co", "mail") # TODO: modify me

# Email message settings
$to = "recipient@domain.com" # TODO: modify me
$subject = "Properties of user %fullname%" # TODO: modify me
$from = "noreply@domain.com" # TODO: Modify me
$mailServer = "mail.domain.com" # TODO: Modify me
$body = "Properties of user %fullname%" # TODO: Modify me

$propertyNameToValue = @{}
foreach ($propertyName in $propertiesToExport)
{
    try
    {
        $value = $Context.TargetObject.Get($propertyName)
    }
    catch
    {
        $value = $NULL
    }
    
    $propertyNameToValue.Add($propertyName, $value)
}

$record = New-Object PSObject -Property $propertyNameToValue
@($record) | Export-Csv -Path $csvFilePath -NoTypeInformation

# Send mail
Send-MailMessage -to $to -From $from -Subject $subject -Body $body -SmtpServer $mailServer -Attachments $csvFilePath

if ($removeCsvFile)
{
    # Remove CSV File
    Remove-Item $csvFilePath -Force
}

Comments ( 0 )
No results found.
Leave a comment