A quick shortcut; since $NewUserParams
is already a hash, cast it to a PSCustom object.
$NewObject = [PSCustomObject]$NewUserParams
At that point you’ve got all sorts of options including ConvertTo-Html
.
Personally I like to pipe to Format-List | OutString
Then just slap a <PRE>...</PRE>
around it. I like to use a mono-spaced font so the HTML will have the same table or list spacing just like you get in the PowerShell console. I frequently do this with Format-Table as well.
Reading through the ConvertTo-Html
documentation it has the following:
Converts a .NET object into HTML that can be displayed in a Web browser.
The reason behind this behavior is Hashtables have key and value pairs opposed to PSCustomObject which is properties, so ConvertTo-Html
treats them differently and you get the behavior you are experiencing.
#$ADObject | ForEach-Object {
$NewUserParams = @{
SamAccountName = $username
Name = "$firstname $surname"
DisplayName = "$firstname $surname"
UserPrincipalName = "[email protected]"
GivenName = $firstname
Surname = $surname
AccountPassword = $securePassword
Enabled = $false
Path = "OU=$NewUserOU,$domainpath" #change to switch based of Users Branch
City = $_.City
Country = $_.Country #NOTE: This Field must be the 2 digit Country Code, NOT the String Name of athe Country.
company = $_.CompanyName
department = $_.OrgDepartmentName
Employeeid = $_.EmployeeId
mobile = $_.Mobile
Manager = $_.Manager
Office = $_.Branch
postalCode = $_.PostalCode
POBox = $_.PostOfficeBox
scriptPath = $_.scriptPath
StreetAddress = $_.StreetAddress
Title = $_.Title
}
try {
#Create the Nww user, and make them change password on first Logon
New-ADUser @NewUserParams -ErrorAction Stop
[PSCustomObject]$NewUserParams = $NewUserParams
Set-ADUser -Identity $username -ChangePasswordAtLogon $true
# $CurrentAttributes = Get-ADUser -Identity $username -Properties *
[void]$mailBody.add("<h1>NEW User Created</h1> $($newUserParams| ConvertTo-Html)")
#Send-MailMessage @MailParams
Write-Host "email sent to Admin"
}
catch {
throw
Write-Warning "Could not create account $username. $($_.Exception.Message)"
}
}
if ($mailbody -ne @()) {
Write-Host "Change Added to mail"
"$(Get-Timestamp )USER Attribute Change $mailbody" | Out-File c:log.txt -Append
Send-MailMessage @MailParams -BodyAsHtml -body "<b>$(Get-TimeStamp): USER UPDATE </b> <p>$mailbody</p>"
}
}
Successful output of generating HTML:

hi upon creation of a new user im wanting to take the $newuserParams, and put them in a nice formatted email. with each parameter on a new line.
ive got either the splatted params outputting all as one big line with | out-string, or i get this
IsReadOnly IsFixedSize IsSynchronized Keys Values SyncRoot Count
False False False System.Collections.Hashtable+KeyCollection System.Collections.Hashtable+ValueCollection System.Object 22
if i use ConvertTo-HTML
i basically want to do both but chaingin both pipes doesn’t work haha, how do i make the array populate so that i can use the ConvertTo-HTML
i want to keep it part of the $mailbody.add, as i have other things adding to $mailbody.
$NewUserParams = @{
SamAccountName = $username
Name = "$firstname $surname"
DisplayName = "$firstname $surname"
UserPrincipalName = "[email protected]"
GivenName = $firstname
Surname = $surname
AccountPassword = $securePassword
Enabled = $false
Path = "OU=$NewUserOU,$domainpath" #change to switch based of Users Branch
City = $_.City
Country = $_.Country #NOTE: This Feild must be the 2 digit Country Code, NOT the String Name of athe Country.
company = $_.CompanyName
department = $_.OrgDepartmentName
Employeeid = $_.EmployeeId
mobile = $_.Mobile
Manager = $_.Manager
Office = $_.Branch
postalCode = $_.PostalCode
POBox = $_.PostOfficeBox
scriptPath = $_.scriptPath
StreetAddress = $_.StreetAddress
Title = $_.Title
}
try {
#Create the NEw user, and make them change password on first Logon
New-ADUser @NewUserParams -ErrorAction Stop
Set-ADUser -Identity $username -ChangePasswordAtLogon $true
# $CurrentAttributes = Get-ADUser -Identity $username -Properties *
[void]$mailBody.add("<h1>NEW User Created</h1> $($newUserParams| ConvertTo-Html)")
#Send-MailMessage @MailParams
Write-Host "email sent to Admin"
}
catch {
throw
Write-Warning "Could not create account $username. $($_.Exception.Message)"
}
}
if ($mailbody -ne @()) {
Write-Host "Change Added to mail"
"$(Get-Timestamp )USER Attribute Change $mailbody" | Out-File c:log.txt -Append
Send-MailMessage @MailParams -BodyAsHtml -body "<b>$(Get-TimeStamp): USER UPDATE </b> <p>$mailbody</p>"
}
}
the $_, is part of a for each user in the .csv file, and the $mailbody.add adds the text when there is a change to an array list($mailbody) that at the end is output as the contents of the email and appends to the log file where i call $mailbody at the end.
I think you have to create the PSCustomObject after the hash is referenced by the