Export like this:
$currentdomains |
ForEach-Object { return [PSCustomObject]@{Domain = $_}} |
Export-Csv ...
Explanation: Export-Csv exports object's properties, not objects itself.
Because your $currentdomains is list of System.Strings, and System.String contains only Length property, it is exported.
So, to export string value, you must export some object, which have property Domain (or anything else you want) and value of this property is string of interest.
The easiest way to create this object in PowerShell is to use covretion from System.Hashtable (also known as key-value collection @{ }) to PSCustomObject. Keys become properties of PSCustomObject and Values become values of it's properties.
So, with ForEach-Object { return [PSCustomObject]@{Domain = $_}} you wrap your string into PSCustomObject object, which gives you Object with single (maybe multiple in other case) property Domain.
Export-Csv enumerates properties of PSCustomObject, creates columns from their names (in this case you have only Domain property, so only one column Domain will be in CSV) and as a value, it uses property's .ToString() method.
Your Domain property has a type System.String, so System.String::ToString() returns string itself.
Alternative way:
(Get-TransportRule "domain block") |
ForEach-Object { return [PSCustomObject]@{Domain = $_.SenderDomainIs}} |
Export-Csv ...
Alternative way:
(Get-TransportRule "domain block") |
Select-Object -Property @(
@{
Label = 'Domain'
Expression = { return $_.SenderDomainIs }
},
'SenderDomainIs' # This line is for example:
# There can be other properties from Get-TransportRule, by their name, like "SenderDomainIs" or by Label-Expression conversion
) |
Export-Csv ...
$CurrentDomains = Get-TransportRule "domain block" | Select-Object -Property SenderDomainIsshould do the trick actually. If you want to rename the property you can use a calculated property to change the header to whatever you like. ;-)[PSCustomObject]. ;-)